/** Method for checking if the message has attachments. */ public boolean hasAttachments() throws java.io.IOException, MessagingException { boolean hasAttachments = false; if (message.isMimeType("multipart/*")) { Multipart mp = (Multipart) message.getContent(); if (mp.getCount() > 1) hasAttachments = true; } return hasAttachments; }
/** Returns the body of the message (if it's plain text). */ public String getBody() throws MessagingException, java.io.IOException { Object content = message.getContent(); if (message.isMimeType("text/plain")) { return (String) content; } else if (message.isMimeType("multipart/alternative")) { Multipart mp = (Multipart) message.getContent(); int numParts = mp.getCount(); for (int i = 0; i < numParts; ++i) { if (mp.getBodyPart(i).isMimeType("text/plain")) return (String) mp.getBodyPart(i).getContent(); } return ""; } else if (message.isMimeType("multipart/*")) { Multipart mp = (Multipart) content; if (mp.getBodyPart(0).isMimeType("text/plain")) return (String) mp.getBodyPart(0).getContent(); else return ""; } else return ""; }
/** * Saves all attachments to a temp directory, and returns the directory path. Null if no * attachments. */ public File saveAttachments(Message message) throws IOException, MessagingException { File tmpDir = Files.createTempDir(); boolean foundAttachments = false; Object content = message.getContent(); if (message.isMimeType(MULTIPART_WILDCARD) && content instanceof Multipart) { Multipart mp = (Multipart) content; for (int i = 0; i < mp.getCount(); i++) { BodyPart bodyPart = mp.getBodyPart(i); if (bodyPart instanceof MimeBodyPart && isNotBlank(bodyPart.getFileName())) { MimeBodyPart mimeBodyPart = (MimeBodyPart) bodyPart; mimeBodyPart.saveFile(new File(tmpDir, mimeBodyPart.getFileName())); foundAttachments = true; } } } return foundAttachments ? tmpDir : null; }
/** Method for checking if the message has the desired mime type. */ public boolean hasMimeType(String mimeType) throws MessagingException { return message.isMimeType(mimeType); }