List of usage examples for javax.mail.internet MimePart writeTo
public void writeTo(OutputStream os) throws IOException, MessagingException;
From source file:mitm.common.mail.MailUtils.java
/** * Saves the MimePart to the output stream * @param part//from w w w. j a va2 s . c o m * @param output * @throws IOException * @throws MessagingException */ public static void writeMessage(MimePart part, OutputStream output) throws IOException, MessagingException { /* * we need to store the result in a temporary buffer so we can fall back * on a different procedure when writeTo fails. If not, MimePart#writeTo might * fail halfway and some data has already been written to output. */ DeferredFileOutputStream buffer = new DeferredFileOutputStream(SizeUtils.MB, FileConstants.TEMP_FILE_PREFIX, null, null); /* * First try the writeTo method. Sometimes this will fail when the message uses * an unsupported or corrupt encoding. For example the content encoding says that * the message is base64 encoded but it is not. */ try { part.writeTo(buffer); /* * Need to close the DeferredFileOutputStream before we can write */ buffer.close(); /* * writeTo was successful so we can copy the result to the final output */ buffer.writeTo(output); } catch (IOException e) { writeMessageRaw(part, output); } catch (MessagingException e) { writeMessageRaw(part, output); } finally { /* * Delete any possible temp file used by DeferredFileOutputStream. */ FileUtils.deleteQuietly(buffer.getFile()); } }