List of usage examples for javax.mail Multipart getBodyPart
public synchronized BodyPart getBodyPart(int index) throws MessagingException
From source file:com.cubusmail.mail.text.MessageTextUtil.java
/** * @param part/* w w w . j ava2 s. c o m*/ * @param messageHandler * @param loadImages * @param reply * @throws MessagingException * @throws IOException */ public static void messageTextFromPart(Part part, MessageHandler messageHandler, boolean loadImages, MessageTextMode mode, Preferences preferences, int level) throws MessagingException, IOException { log.debug("Content type of part: " + part.getContentType()); if (mode == MessageTextMode.DISPLAY || mode == MessageTextMode.DRAFT) { if (MessageUtils.isImagepart(part)) { messageHandler.setMessageImageHtml(createImageMessageText(messageHandler.getId())); } else if (!preferences.isShowHtml() && !StringUtils.isEmpty(messageHandler.getMessageTextPlain())) { return; } else if (preferences.isShowHtml() && !StringUtils.isEmpty(messageHandler.getMessageTextHtml())) { return; } else if (part.isMimeType("text/plain")) { String text = readPart(part); if (!StringUtils.isBlank(text)) { messageHandler.setMessageTextPlain(formatPlainText(text, mode)); } } else if (part.isMimeType("text/html")) { if (preferences.isShowHtml()) { String text = readPart(part); boolean[] hasImages = new boolean[] { false }; if (!StringUtils.isBlank(convertHtml2PlainText(text))) { text = formatHTMLText(text, loadImages, hasImages); messageHandler.setMessageTextHtml(text); messageHandler.setHtmlMessage(true); messageHandler.setHasImages(hasImages[0]); } } else { // only if there is no plain text part found if (StringUtils.isEmpty(messageHandler.getMessageTextPlain())) { String text = readPart(part); text = convertHtml2PlainText(text); if (!StringUtils.isBlank(text)) { text = formatPlainText(text, mode); messageHandler.setMessageTextPlain(text); } } } } else if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); int count = mp.getCount(); for (int i = 0; i < count; i++) { Part subPart = mp.getBodyPart(i); messageTextFromPart(subPart, messageHandler, loadImages, mode, preferences, level++); } } } else if (mode == MessageTextMode.REPLY) { if (!preferences.isCreateHtmlMsgs() && !StringUtils.isEmpty(messageHandler.getMessageTextPlain())) { return; } else if (preferences.isCreateHtmlMsgs() && !StringUtils.isEmpty(messageHandler.getMessageTextHtml())) { return; } else if (part.isMimeType("text/plain")) { String text = readPart(part); text = quotePlainText(text); if (preferences.isCreateHtmlMsgs()) { text = convertPlainText2Html(text, mode); messageHandler.setMessageTextHtml(text); messageHandler.setHtmlMessage(true); } else { messageHandler.setMessageTextPlain(text); } } else if (part.isMimeType("text/html") && StringUtils.isEmpty(messageHandler.getMessageTextPlain())) { String text = readPart(part); text = convertHtml2PlainText(text); text = quotePlainText(text); if (preferences.isCreateHtmlMsgs()) { text = convertPlainText2Html(text, mode); messageHandler.setMessageTextHtml(text); messageHandler.setHtmlMessage(true); } else { messageHandler.setMessageTextPlain(text); } } else if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); int count = mp.getCount(); for (int i = 0; i < count; i++) { Part subPart = mp.getBodyPart(i); messageTextFromPart(subPart, messageHandler, loadImages, mode, preferences, level++); } } } }
From source file:mitm.common.mail.BodyPartUtils.java
/** * Searches for an embedded RFC822 messages. Returns the first embedded RFC822 it finds. * Only one level deep is searched./*from w w w . j a v a 2s . c om*/ * * Returns null if there are no embedded message. */ public static MimeMessage searchForRFC822(MimeMessage message) throws MessagingException, IOException { /* * Fast fail. Only multipart mixed messages are supported. */ if (!message.isMimeType("multipart/mixed")) { return null; } Multipart mp; try { mp = (Multipart) message.getContent(); } catch (IOException e) { throw new MessagingException("Error getting message content.", e); } MimeMessage embeddedMessage = null; for (int i = 0; i < mp.getCount(); i++) { BodyPart part = mp.getBodyPart(i); if (part.isMimeType("message/rfc822")) { embeddedMessage = BodyPartUtils.extractFromRFC822(part); break; } } return embeddedMessage; }
From source file:org.elasticsearch.river.email.EmailToJson.java
public static List<AttachmentInfo> saveAttachmentToWeedFs(Part message, List<AttachmentInfo> attachments, EmailRiverConfig config)/*ww w . j av a 2 s . co 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.stimulus.archiva.extraction.MessageExtraction.java
private static void dumpPart(Part p, Hashtable<String, Part> attachments, Hashtable<String, String> inlines, Hashtable<String, String> images, Hashtable<String, String> nonImages, ArrayList<String> mimeTypes, String subject) throws Exception { mimeTypes.add(p.getContentType());//from w w w . j av a 2s.c om if (!p.isMimeType("multipart/*")) { String disp = p.getDisposition(); String fname = p.getFileName(); if (fname != null) { try { fname = MimeUtility.decodeText(fname); } catch (Exception e) { logger.debug("cannot decode filename:" + e.getMessage()); } } if ((disp != null && Compare.equalsIgnoreCase(disp, Part.ATTACHMENT)) || fname != null) { String filename = getFilename(subject, p); if (!filename.equalsIgnoreCase("winmail.dat")) { attachments.put(filename, p); } /*if (p.isMimeType("image/*")) { String str[] = p.getHeader("Content-ID"); if (str != null) images.put(filename,str[0]); else images.put(filename, filename); } return;*/ } } if (p.isMimeType("text/plain")) { String str = ""; if (inlines.containsKey("text/plain")) str = (String) inlines.get("text/plain") + "\n\n-------------------------------\n"; inlines.put("text/plain", str + getTextContent(p)); } else if (p.isMimeType("text/html")) { inlines.put("text/html", getTextContent(p)); } else if (p.isMimeType("text/xml")) { attachments.put(getFilename(subject, p), p); } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); i++) dumpPart(mp.getBodyPart(i), attachments, inlines, images, nonImages, mimeTypes, subject); } else if (p.isMimeType("message/rfc822")) { dumpPart((Part) p.getContent(), attachments, inlines, images, nonImages, mimeTypes, subject); } else if (p.isMimeType("application/ms-tnef")) { Part tnefpart = TNEFMime.convert(null, p, false); if (tnefpart != null) { dumpPart((Part) tnefpart, attachments, inlines, images, nonImages, mimeTypes, subject); } } else if (p.isMimeType("application/*")) { String filename = getFilename("application", p); attachments.put(filename, p); String str[] = p.getHeader("Content-ID"); if (str != null) nonImages.put(filename, str[0]); } else if (p.isMimeType("image/*")) { String fileName = getFilename("image", p); attachments.put(fileName, p); String str[] = p.getHeader("Content-ID"); if (str != null) images.put(fileName, str[0]); else images.put(fileName, fileName); } else { String contentType = p.getContentType(); Object o = p.getContent(); if (o instanceof String) { String str = ""; if (inlines.containsKey("text/plain")) str = (String) inlines.get("text/plain") + "\n\n-------------------------------\n"; inlines.put(contentType, str + (String) o); } else { String fileName = getFilenameFromContentType("attach", contentType); attachments.put(fileName, p); } } }
From source file:com.panet.imeta.job.entries.getpop.JobEntryGetPOP.java
public static void handleMultipart(String foldername, Multipart multipart) throws MessagingException, IOException { for (int i = 0, n = multipart.getCount(); i < n; i++) { handlePart(foldername, multipart.getBodyPart(i)); }/*from ww w . j av a 2 s . c o m*/ }
From source file:com.cubusmail.mail.util.MessageUtils.java
/** * @param part/*from w ww .j a va 2 s .c o m*/ * @return * @throws MessagingException * @throws IOException */ public static boolean hasAttachments(Part part) throws MessagingException, IOException { try { if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); for (int i = 0; i < mp.getCount(); i++) { MimeBodyPart bodyPart = (MimeBodyPart) mp.getBodyPart(i); if (Part.ATTACHMENT.equals(bodyPart.getDisposition()) || Part.INLINE.equals(bodyPart.getDisposition())) { return true; } } } } catch (Exception e) { log.error(e.getMessage(), e); } return false; }
From source file:com.cubusmail.mail.util.MessageUtils.java
/** * Method for checking if the message has attachments. *///from w w w. j av a 2 s.c o m public static List<MimePart> attachmentsFromPart(Part part) throws MessagingException, IOException { List<MimePart> attachmentParts = new ArrayList<MimePart>(); if (part instanceof MimePart) { MimePart mimePart = (MimePart) part; if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) mimePart.getContent(); for (int i = 0; i < mp.getCount(); i++) { MimeBodyPart bodyPart = (MimeBodyPart) mp.getBodyPart(i); if (Part.ATTACHMENT.equals(bodyPart.getDisposition()) || Part.INLINE.equals(bodyPart.getDisposition())) { attachmentParts.add(bodyPart); } } } else if (part.isMimeType("application/*")) { attachmentParts.add(mimePart); } } return attachmentParts; }
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;//from w ww .j a v a2 s .c om 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.aurel.track.util.emailHandling.MailReader.java
/** * Process multipart e-mail/*from w w w . j a v a 2 s . c o m*/ */ 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.MessageUtils.java
/** * Handle the parts of the given message. The method will call itself * recursively to handle all nested parts * * @param message the MimeMessage//from ww w .ja va 2 s.c o m * @param content the current processing Content * @param sbPlain the StringBuffer to fill with text * @param attachmentList ArrayList with attachments * @throws UnsupportedEncodingException * @throws MessagingException * @throws IOException */ public static boolean handleParts(Message message, Object content, StringBuffer sbPlain, ArrayList<MessageAttachment> attachmentList) throws UnsupportedEncodingException, MessagingException, IOException { boolean isHTML = false; if (content instanceof String) { if (message.getContentType().toLowerCase().startsWith("text/html")) { isHTML = true; } else { isHTML = false; } sbPlain.append((String) content); } else if (content instanceof Multipart) { Multipart mp = (Multipart) content; String multipartContentType = mp.getContentType().toLowerCase(); String text = null; if (multipartContentType.startsWith("multipart/alternative")) { isHTML = handleMultiPartAlternative(mp, sbPlain); } else { for (int i = 0; i < mp.getCount(); i++) { Part part = mp.getBodyPart(i); String contentType = part.getContentType().toLowerCase(); Boolean bodyRead = sbPlain.length() > 0; if (!bodyRead && contentType.startsWith("text/plain")) { isHTML = false; text = (String) part.getContent(); } else if (!bodyRead && contentType.startsWith("text/html")) { isHTML = true; text = (String) part.getContent(); } else if (!bodyRead && contentType.startsWith("message/rfc822")) { // Extract the message and pass it MimeMessage msg = (MimeMessage) part.getDataHandler().getContent(); isHTML = handleParts(msg, msg.getContent(), sbPlain, attachmentList); } else { if (part.getFileName() != null) { // Inline images are not added to the attachment // list // TODO: improve the in-line images detection if (part.getHeader("Content-ID") == null) { MessageAttachment attachment = new MessageAttachmentImpl(); attachment.setName(MimeUtility.decodeText(part.getFileName())); attachment.setContentType(part.getContentType()); attachment.setSize(part.getSize()); attachmentList.add(attachment); } } else { isHTML = handleParts(message, part.getContent(), sbPlain, attachmentList); } } } if (text != null) sbPlain.append(text); } } return isHTML; }