List of usage examples for javax.mail.internet MimeBodyPart getRawInputStream
public InputStream getRawInputStream() throws MessagingException
From source file:com.cubusmail.mail.text.MessageTextUtil.java
/** * Reads the string out of part's input stream. On first try the input * stream retrieved by <code>javax.mail.Part.getInputStream()</code> is * used. If an I/O error occurs (<code>java.io.IOException</code>) then the * next try is with part's raw input stream. If everything fails an empty * string is returned./*from ww w . j a va 2 s. co m*/ * * @param p * - the <code>javax.mail.Part</code> object * @param ct * - the part's content type * @return the string read from part's input stream or the empty string "" * if everything failed * @throws MessagingException * - if an error occurs in part's getter methods */ public static String readPart(final Part p) throws MessagingException { String contentType = p.getContentType(); ContentType type = new ContentType(contentType); /* * Use specified charset if available else use default one */ String charset = type.getParameter("charset"); if (null == charset || charset.equalsIgnoreCase(CubusConstants.US_ASCII)) { charset = CubusConstants.DEFAULT_CHARSET; } try { return readStream(p.getInputStream(), charset); } catch (final IOException e) { /* * Try to get data from raw input stream */ final InputStream inStream; if (p instanceof MimeBodyPart) { final MimeBodyPart mpb = (MimeBodyPart) p; inStream = mpb.getRawInputStream(); } else if (p instanceof MimeMessage) { final MimeMessage mm = (MimeMessage) p; inStream = mm.getRawInputStream(); } else { inStream = null; } if (inStream == null) { /* * Neither a MimeBodyPart nor a MimeMessage */ return ""; } try { return readStream(inStream, charset); } catch (final IOException e1) { log.error(e1.getLocalizedMessage(), e1); return e1.getLocalizedMessage(); // return STR_EMPTY; } finally { try { inStream.close(); } catch (final IOException e1) { log.error(e1.getLocalizedMessage(), e1); } } } }
From source file:mitm.common.mail.BodyPartUtils.java
public static MimeBodyPart makeContentBodyPartRaw(MimeBodyPart sourceMessage, HeaderMatcher matcher) throws IOException, MessagingException { /*//from ww w . ja v a 2 s.c om * getRawInputStream() can throw a MessagingException when the source message is a MimeMessage * that is created in code (ie. not from a stream) */ InputStream messageStream = sourceMessage.getRawInputStream(); MimeBodyPart newBodyPart = new MimeBodyPart(messageStream); HeaderUtils.copyHeaders(sourceMessage, newBodyPart, matcher); return newBodyPart; }
From source file:mitm.common.mail.BodyPartUtils.java
/** * This is the only way I know of to create a new MimeBodyPart from another MimeBodyPart which is safe * for signed email. All other methods break the signature when quoted printable soft breaks are used. * /*from ww w.ja va 2s . c o m*/ * example of quoted printable soft breaks: * * Content-Transfer-Encoding: quoted-printable * soft break example = * another line = * * All other methods will re-encode and removes the soft breaks. * * @param sourceMessage * @param matcher * @return * @throws IOException * @throws MessagingException */ @SuppressWarnings("unchecked") public static MimeBodyPart makeContentBodyPartRawBytes(MimeBodyPart sourceMessage, HeaderMatcher matcher) throws IOException, MessagingException { /* * getRawInputStream() can throw a MessagingException when the source message is a MimeMessage * that is created in code (ie. not from a stream) */ InputStream messageStream = sourceMessage.getRawInputStream(); byte[] rawMessage = IOUtils.toByteArray(messageStream); InternetHeaders destinationHeaders = new InternetHeaders(); Enumeration<Header> sourceHeaders = sourceMessage.getAllHeaders(); HeaderUtils.copyHeaders(sourceHeaders, destinationHeaders, matcher); MimeBodyPart newBodyPart = new MimeBodyPart(destinationHeaders, rawMessage); return newBodyPart; }
From source file:mitm.common.security.smime.SMIMEUtils.java
public static void writeBodyPart(BodyPart bodyPart, OutputStream output, String defaultContentTransferEncoding) throws IOException, MessagingException { if (bodyPart instanceof MimeBodyPart) { MimeBodyPart mimeBodyPart = (MimeBodyPart) bodyPart; String[] contentTransferEncodings = bodyPart.getHeader("Content-Transfer-Encoding"); String contentTransferEncoding = defaultContentTransferEncoding; if (contentTransferEncodings != null && contentTransferEncodings.length > 0) { contentTransferEncoding = contentTransferEncodings[0]; }/*from w ww. ja v a2 s . co m*/ /* * First try the raw input stream. * If message is created from a stream Javamail will return the raw stream. If * the message is created from 'scratch' getRawInputStream throws a * MessagingException. We will therefore first try the raw version and if * that fails we fallback on writeTo. */ try { InputStream input = mimeBodyPart.getRawInputStream(); Enumeration<?> lines = mimeBodyPart.getAllHeaderLines(); /* step through all header lines */ while (lines.hasMoreElements()) { String header = (String) lines.nextElement(); output.write(MiscStringUtils.toAsciiBytes(header)); output.write(MailUtils.CRLF_BYTES); } output.write(MailUtils.CRLF_BYTES); if (!contentTransferEncoding.equalsIgnoreCase("binary")) { output = new CRLFOutputStream(output); } IOUtils.copy(input, output); output.flush(); } catch (MessagingException e) { /* * Fallback to writeTo */ if (!contentTransferEncoding.equalsIgnoreCase("binary")) { output = new CRLFOutputStream(output); } bodyPart.writeTo(output); output.flush(); } } else { if (!defaultContentTransferEncoding.equalsIgnoreCase("binary")) { output = new CRLFOutputStream(output); } bodyPart.writeTo(output); output.flush(); } }
From source file:de.kp.ames.web.function.access.imap.ImapConsumer.java
/** * @param part/*from w ww .j a va 2s. c o m*/ * @return * @throws MessagingException */ private String readPart(Part part) throws MessagingException { try { return readStream(part.getInputStream()); } catch (IOException e) { /* * Try to get message from raw input stream */ final InputStream stream; if (part instanceof MimeBodyPart) { final MimeBodyPart mimeBodyPart = (MimeBodyPart) part; stream = mimeBodyPart.getRawInputStream(); } else if (part instanceof MimeMessage) { final MimeMessage mm = (MimeMessage) part; stream = mm.getRawInputStream(); } else { stream = null; } if (stream == null) { /* * Neither a MimeBodyPart nor a MimeMessage */ return ""; } try { return readStream(stream); } catch (IOException e1) { return ""; } finally { try { stream.close(); } catch (IOException e1) { e1.printStackTrace(); } } } }