List of usage examples for javax.mail Part getContent
public Object getContent() throws IOException, MessagingException;
From source file:Main.java
public static boolean isContainAttachment(Part part) throws MessagingException, IOException { boolean flag = false; if (part.isMimeType("multipart/*")) { MimeMultipart multipart = (MimeMultipart) part.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))) { flag = true;//w w w . j a va2s .c o m } else if (bodyPart.isMimeType("multipart/*")) { flag = isContainAttachment(bodyPart); } else { String contentType = bodyPart.getContentType(); if (contentType.indexOf("application") != -1) { flag = true; } if (contentType.indexOf("name") != -1) { flag = true; } } if (flag) break; } } else if (part.isMimeType("message/rfc822")) { flag = isContainAttachment((Part) part.getContent()); } return flag; }
From source file:org.nuxeo.ecm.core.convert.plugins.text.extractors.RFC822ToTextConverter.java
protected static List<Part> getAttachmentParts(Part p) throws Exception { List<Part> res = new ArrayList<Part>(); if (p.isMimeType(MESSAGE_RFC822_MIMETYPE)) { res.addAll(getAttachmentParts((Part) p.getContent())); } else if (p.isMimeType("multipart/alternative")) { // only return one of the text alternatives Multipart mp = (Multipart) p.getContent(); int count = mp.getCount(); Part alternativePart = null;//w w w . j a v a2 s .com for (int i = 0; i < count; i++) { Part subPart = mp.getBodyPart(i); if (subPart.isMimeType(TXT_MT)) { alternativePart = subPart; break; } else if (subPart.isMimeType("text/*")) { alternativePart = subPart; } else { res.addAll(getAttachmentParts(subPart)); } } if (alternativePart != null) { res.add(alternativePart); } } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); int count = mp.getCount(); for (int i = 0; i < count; i++) { res.addAll(getAttachmentParts(mp.getBodyPart(i))); } } else { res.add(p); } return res; }
From source file:org.nuxeo.ecm.core.convert.plugins.text.extractors.RFC822ToTextConverter.java
protected static byte[] extractTextFromMessagePart(Part p) throws Exception { ContentType contentType = new ContentType(p.getContentType()); String baseType = contentType.getBaseType(); if (TXT_MT.equals(baseType)) { Object content = p.getContent(); if (content instanceof String) { return ((String) content).getBytes(); } else {/*from w ww . ja v a 2 s.com*/ return null; } } ConversionService cs = Framework.getLocalService(ConversionService.class); String converterName = cs.getConverterName(baseType, TXT_MT); if (converterName == null) { return null; } else { BlobHolder result = cs.convert(converterName, new SimpleBlobHolder(new FileBlob(p.getInputStream())), null); return result.getBlob().getByteArray(); } }
From source file:com.email.ReceiveEmail.java
/** * Gather the email body//from w w w. ja v a 2 s . c om * * @param p Part * @return String body */ private static String getEmailBodyText(Part p) { try { if (p.isMimeType("text/*")) { String s = (String) p.getContent(); return s; } if (p.isMimeType("multipart/alternative")) { // prefer html text over plain text Multipart mp = (Multipart) p.getContent(); String text = null; for (int i = 0; i < mp.getCount(); i++) { Part bp = mp.getBodyPart(i); if (bp.isMimeType("text/plain")) { if (text == null) { text = getEmailBodyText(bp); } } else if (bp.isMimeType("text/html")) { String s = getEmailBodyText(bp); if (s != null) { return s; } } else { return getEmailBodyText(bp); } } return text; } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); i++) { String s = getEmailBodyText(mp.getBodyPart(i)); if (s != null) { return s; } } } return ""; } catch (MessagingException | IOException ex) { ExceptionHandler.Handle(ex); } return ""; }
From source file:javamailclient.GmailAPI.java
/** * Return the primary text content of the message. *///from w w w .jav a 2 s .c om private static String getMessageBody(Part p) throws MessagingException, IOException { if (p.isMimeType("text/*")) { String s = (String) p.getContent(); return s; } if (p.isMimeType("multipart/alternative")) { // prefer html text over plain text Multipart mp = (Multipart) p.getContent(); String text = null; for (int i = 0; i < mp.getCount(); i++) { Part bp = mp.getBodyPart(i); if (bp.isMimeType("text/plain")) { if (text == null) { text = getMessageBody(bp); } continue; } else if (bp.isMimeType("text/html")) { String s = getMessageBody(bp); if (s != null) { return s; } } else { return getMessageBody(bp); } } return text; } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); i++) { String s = getMessageBody(mp.getBodyPart(i)); if (s != null) { return s; } } } return null; }
From source file:mitm.common.mail.BodyPartUtils.java
private static String getPlainBodyAndAttachments(final Part part, Collection<Part> attachments, int level, int maxLevel) throws MessagingException, IOException { String body = null;//from www . j a va 2s . com if (part.isMimeType("text/plain")) { body = (String) part.getContent(); } else { /* * Maximum level deep */ if (level <= maxLevel && part.isMimeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); int partCount = mp.getCount(); for (int i = 0; i < partCount; i++) { Part child = mp.getBodyPart(i); if (body == null) { body = getPlainBodyAndAttachments(child, attachments, level + 1, maxLevel); if (body == null && part.isMimeType("multipart/mixed")) { if (attachments != null) { attachments.add(child); } } } else if (part.isMimeType("multipart/mixed")) { if (attachments != null) { attachments.add(child); } } } } } return body; }
From source file:uidmsgshow.java
public static void dumpPart(Part p) throws Exception { if (p instanceof Message) dumpEnvelope((Message) p);//w w w . j av a 2 s .c om /* Dump input stream InputStream is = new BufferedInputStream(p.getInputStream()); int c; while ((c = is.read()) != -1) System.out.write(c); */ System.out.println("CONTENT-TYPE: " + p.getContentType()); Object o = p.getContent(); if (o instanceof String) { System.out.println("This is a String"); System.out.println("---------------------------"); System.out.println((String) o); } else if (o instanceof Multipart) { System.out.println("This is a Multipart"); System.out.println("---------------------------"); Multipart mp = (Multipart) o; int count = mp.getCount(); for (int i = 0; i < count; i++) dumpPart(mp.getBodyPart(i)); } else if (o instanceof Message) { System.out.println("This is a Nested Message"); System.out.println("---------------------------"); dumpPart((Part) o); } else if (o instanceof InputStream) { System.out.println("This is just an input stream"); System.out.println("---------------------------"); InputStream is = (InputStream) o; int c; while ((c = is.read()) != -1) System.out.write(c); } }
From source file:org.apache.hupa.server.utils.MessageUtils.java
private static boolean handleMultiPartAlternative(Multipart mp, StringBuffer sbPlain) throws MessagingException, IOException { String text = null;/* w ww . j ava 2 s . co m*/ boolean isHTML = false; for (int i = 0; i < mp.getCount(); i++) { Part part = mp.getBodyPart(i); String contentType = part.getContentType().toLowerCase(); // we prefer html if (text == null && contentType.startsWith("text/plain")) { isHTML = false; text = (String) part.getContent(); } else if (contentType.startsWith("text/html")) { isHTML = true; text = (String) part.getContent(); } } sbPlain.append(text); return isHTML; }
From source file:uidmsgshow.java
public static void dumpPart(Part p) throws Exception { if (p instanceof Message) dumpEnvelope((Message) p);// w ww .ja v a 2 s . c o m /* * Dump input stream InputStream is = new * BufferedInputStream(p.getInputStream()); int c; while ((c = is.read()) != * -1) System.out.write(c); */ System.out.println("CONTENT-TYPE: " + p.getContentType()); Object o = p.getContent(); if (o instanceof String) { System.out.println("This is a String"); System.out.println("---------------------------"); System.out.println((String) o); } else if (o instanceof Multipart) { System.out.println("This is a Multipart"); System.out.println("---------------------------"); Multipart mp = (Multipart) o; int count = mp.getCount(); for (int i = 0; i < count; i++) dumpPart(mp.getBodyPart(i)); } else if (o instanceof Message) { System.out.println("This is a Nested Message"); System.out.println("---------------------------"); dumpPart((Part) o); } else if (o instanceof InputStream) { System.out.println("This is just an input stream"); System.out.println("---------------------------"); InputStream is = (InputStream) o; int c; while ((c = is.read()) != -1) System.out.write(c); } }
From source file:org.apache.hupa.server.utils.MessageUtils.java
/** * Loop over MuliPart and write the content to the Outputstream if a * attachment with the given name was found. * * @param logger//from w w w.j a va 2 s .c om * The logger to use * @param content * Content which should checked for attachments * @param attachmentName * The attachmentname or the unique id for the searched attachment * @throws MessagingException * @throws IOException */ public static Part handleMultiPart(Log logger, Object content, String attachmentName) throws MessagingException, IOException { if (content instanceof Multipart) { Multipart part = (Multipart) content; for (int i = 0; i < part.getCount(); i++) { Part bodyPart = part.getBodyPart(i); String fileName = bodyPart.getFileName(); String[] contentId = bodyPart.getHeader("Content-ID"); if (bodyPart.isMimeType("multipart/*")) { Part p = handleMultiPart(logger, bodyPart.getContent(), attachmentName); if (p != null) return p; } else { if (contentId != null) { for (String id : contentId) { id = id.replaceAll("^.*<(.+)>.*$", "$1"); System.out.println(attachmentName + " " + id); if (attachmentName.equals(id)) return bodyPart; } } if (fileName != null) { if (cleanName(attachmentName).equalsIgnoreCase(cleanName(MimeUtility.decodeText(fileName)))) return bodyPart; } } } } else { logger.error("Unknown content: " + content.getClass().getName()); } return null; }