List of usage examples for javax.mail Part getContent
public Object getContent() throws IOException, MessagingException;
From source file:com.aurel.track.util.emailHandling.MailReader.java
private static String hadleMultipartAlternative(Part part, List<EmailAttachment> attachments, boolean ignoreAttachments) throws IOException, MessagingException { LOGGER.debug("hadleMultipartAlternative"); // prefer HTML text over plain text Multipart mp = (Multipart) part.getContent(); String plainText = null;// ww w .ja v a 2 s . co m String otherText = null; for (int i = 0; i < mp.getCount(); i++) { Part bp = mp.getBodyPart(i); if (bp.isMimeType("text/html")) { return getText(bp); } else if (bp.isMimeType("text/plain")) { plainText = getText(bp); } else { LOGGER.debug("Process alternative body part having mimeType:" + bp.getContentType()); otherText = handlePart(bp, attachments, ignoreAttachments).toString(); } } if (otherText != null) { return otherText; } else { return plainText; } }
From source file:com.email.ReceiveEmail.java
/** * Save the attachments from the email//from w ww. j a v a 2s .co m * * @param p Part * @param m Message * @param eml EmailMessageModel */ private static void saveAttachments(Part p, Message m, EmailMessageModel eml) { try { String filename = p.getFileName(); if (filename != null && !filename.endsWith("vcf")) { try { saveFile(p, filename, eml); } catch (ClassCastException ex) { System.err.println("CRASH"); } } else if (p.isMimeType("IMAGE/*")) { saveFile(p, filename, eml); } if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); int pageCount = mp.getCount(); for (int i = 0; i < pageCount; i++) { saveAttachments(mp.getBodyPart(i), m, eml); } } else if (p.isMimeType("message/rfc822")) { saveAttachments((Part) p.getContent(), m, eml); } } catch (IOException | MessagingException ex) { ExceptionHandler.Handle(ex); } }
From source file:com.aurel.track.util.emailHandling.MailReader.java
/** * Process multipart e-mail/*from w w w. j a v a 2s .c om*/ */ private static String handleMultipart(Part part, List<EmailAttachment> attachments, boolean ignoreAttachments) throws MessagingException, IOException { if (part.isMimeType("multipart/alternative")) { return hadleMultipartAlternative(part, attachments, ignoreAttachments); } else if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < mp.getCount(); i++) { StringBuffer s = handlePart(mp.getBodyPart(i), attachments, ignoreAttachments); if (s != null) { sb.append(s); } } return sb.toString(); } return null; }
From source file:org.apache.hupa.server.utils.TestUtils.java
public static ArrayList<String> summaryzeContent(Object content, String contentType, final int spaces) throws IOException, MessagingException { ContenTypeArrayList ret = new ContenTypeArrayList(); ret.add(contentType, spaces);// w w w . ja v a 2s . co m if (content instanceof Multipart) { Multipart mp = (Multipart) content; contentType = mp.getContentType(); for (int i = 0; i < mp.getCount(); i++) { Part part = mp.getBodyPart(i); contentType = part.getContentType(); if (contentType.startsWith("text")) { ret.add(contentType, spaces + 1); } else if (contentType.startsWith("message/rfc822")) { MimeMessage msg = (MimeMessage) part.getDataHandler().getContent(); ret.addAll(summaryzeContent(msg.getContent(), msg.getContentType(), spaces + 1)); } else { if (part.getFileName() != null) { ret.add(part.getContentType(), part.getFileName(), spaces + 1); } else { ret.addAll(summaryzeContent(part.getContent(), contentType, spaces + 1)); } } } } return ret; }
From source file:de.saly.elasticsearch.support.IndexableMailMessage.java
private static String getText(final Part p, int depth) throws MessagingException, IOException { if (depth >= 100) { throw new IOException("Endless recursion detected "); }/*from w w w .j av a 2 s . co m*/ // TODO fix encoding for buggy encoding headers if (p.isMimeType("text/*")) { Object content = null; try { content = p.getContent(); } catch (final Exception e) { logger.error("Unable to index the content of a message due to {}", e.toString()); return null; } if (content instanceof String) { final String s = (String) p.getContent(); return s; } if (content instanceof InputStream) { final InputStream in = (InputStream) content; // TODO guess encoding with // http://code.google.com/p/juniversalchardet/ final String s = IOUtils.toString(in, "UTF-8"); IOUtils.closeQuietly(in); return s; } throw new MessagingException("Unknown content class representation: " + content.getClass()); } if (p.isMimeType("multipart/alternative")) { // prefer plain text over html text final Multipart mp = (Multipart) p.getContent(); String text = null; for (int i = 0; i < mp.getCount(); i++) { final Part bp = mp.getBodyPart(i); if (bp.isMimeType("text/html")) { if (text == null) { text = getText(bp, ++depth); } continue; } else if (bp.isMimeType("text/plain")) { final String s = getText(bp, ++depth); if (s != null) { return s; } } else { return getText(bp, ++depth); } } return text; } else if (p.isMimeType("multipart/*")) { final Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); i++) { final String s = getText(mp.getBodyPart(i), ++depth); if (s != null) { return s; } } } return null; }
From source file:com.it.j2ee.email.MailServiceTest.java
private String getMainPartText(Part mainPart) throws MessagingException, IOException { return (String) ((Multipart) mainPart.getContent()).getBodyPart(0).getContent(); }
From source file:de.saly.elasticsearch.importer.imap.support.IndexableMailMessage.java
private static String getText(final Part p, int depth, final boolean preferHtmlContent) throws MessagingException, IOException { if (depth >= 100) { throw new IOException("Endless recursion detected "); }// w w w .j a v a2s . co m // TODO fix encoding for buggy encoding headers if (p.isMimeType("text/*")) { Object content = null; try { content = p.getContent(); } catch (final Exception e) { logger.error("Unable to index the content of a message due to {}", e.toString()); return null; } if (content instanceof String) { final String s = (String) p.getContent(); return s; } if (content instanceof InputStream) { final InputStream in = (InputStream) content; // TODO guess encoding with // http://code.google.com/p/juniversalchardet/ final String s = IOUtils.toString(in, "UTF-8"); IOUtils.closeQuietly(in); return s; } throw new MessagingException("Unknown content class representation: " + content.getClass()); } if (p.isMimeType("multipart/alternative")) { // prefer plain text over html text final Multipart mp = (Multipart) p.getContent(); String text = null; for (int i = 0; i < mp.getCount(); i++) { final Part bp = mp.getBodyPart(i); if (bp.isMimeType("text/html")) { if (text == null) { text = getText(bp, ++depth, preferHtmlContent); } if (preferHtmlContent) { return text; } else { continue; } } else if (bp.isMimeType("text/plain")) { final String s = getText(bp, ++depth, preferHtmlContent); if (s != null && !preferHtmlContent) { return s; } else { continue; } } else { return getText(bp, ++depth, preferHtmlContent); } } return text; } else if (p.isMimeType("multipart/*")) { final Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); i++) { final String s = getText(mp.getBodyPart(i), ++depth, preferHtmlContent); if (s != null) { return s; } } } return null; }
From source file:org.elasticsearch.river.email.EmailToJson.java
public static List<AttachmentInfo> saveAttachmentToWeedFs(Part message, List<AttachmentInfo> attachments, EmailRiverConfig config)//from w ww.ja v a2 s. c o m throws UnsupportedEncodingException, MessagingException, FileNotFoundException, IOException { if (attachments == null) { attachments = new ArrayList<AttachmentInfo>(); } boolean hasAttachment = false; try { hasAttachment = isContainAttachment(message); } catch (MessagingException e) { logger.error("save attachment", e); } catch (IOException e) { logger.error("save attachment", e); } if (hasAttachment) { if (message.isMimeType("multipart/*")) { Multipart multipart = (Multipart) message.getContent(); int partCount = multipart.getCount(); for (int i = 0; i < partCount; i++) { BodyPart bodyPart = multipart.getBodyPart(i); String disp = bodyPart.getDisposition(); if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) { InputStream is = bodyPart.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len = -1; while ((len = bis.read()) != -1) { bos.write(len); } bos.close(); bis.close(); byte[] data = bos.toByteArray(); String fileId = uploadFileToWeedfs(data, config); if (fileId != null) { AttachmentInfo info = new AttachmentInfo(); info.fileId = fileId; info.fileName = decodeText(bodyPart.getFileName()); info.fileSize = data.length; attachments.add(info); } } else if (bodyPart.isMimeType("multipart/*")) { attachments.addAll(saveAttachmentToWeedFs(bodyPart, attachments, config)); } else { String contentType = bodyPart.getContentType(); if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) { attachments.addAll(saveAttachmentToWeedFs(bodyPart, attachments, config)); } } } } else if (message.isMimeType("message/rfc822")) { attachments.addAll(saveAttachmentToWeedFs(message, attachments, config)); } } return attachments; }
From source file:com.ikon.util.MailUtils.java
/** * Get text from message//from ww w . jav a2 s . com */ private static String getText(Part p) throws MessagingException, IOException { if (p.isMimeType("text/*")) { Object obj = p.getContent(); String str = NO_BODY; if (obj instanceof InputStream) { InputStream is = (InputStream) obj; StringWriter writer = new StringWriter(); IOUtils.copy(is, writer, "UTF-8"); str = writer.toString(); } else { str = (String) obj; } if (p.isMimeType("text/html")) { return "H" + str; } else if (p.isMimeType("text/plain")) { return "T" + str; } else { // Otherwise let's set as text/plain return "T" + str; } } else if (p.isMimeType("multipart/alternative")) { // prefer html over plain text Multipart mp = (Multipart) p.getContent(); String text = "T" + NO_BODY; // log.info("Mime Parts: {}", mp.getCount()); for (int i = 0; i < mp.getCount(); i++) { Part bp = mp.getBodyPart(i); if (bp.isMimeType("text/plain")) { text = getText(bp); } else if (bp.isMimeType("text/html")) { text = getText(bp); break; } else { text = getText(bp); } } return text; } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); i++) { String s = getText(mp.getBodyPart(i)); if (s != null) return s; } } return "T" + NO_BODY; }
From source file:org.nuxeo.ecm.platform.mail.action.TransformMessageAction.java
private void addPartsToTextMessage(List<Part> someMessageParts) throws MessagingException, IOException { for (Part part : someMessageParts) { addToTextMessage(part.getContent().toString(), contentTypeIsPlainText(part.getContentType())); }// w ww . ja v a2s . c o m }