public MessagesWrapper print() throws MessagingException { logger.info("Found message(s) : " + messages.size()); for (Message message : messages) { logger.info(">>>>>>"); logger.info("Date : " + message.getSentDate()); logger.info("From : " + (message.getFrom().length > 0 ? message.getFrom()[0] : null)); logger.info("Subject : " + message.getSubject()); logger.info("<<<<<<"); } return this; }
public MessagesWrapper markAsRead(List<Message> messagez) throws IOException, MessagingException { if (folder.isOpen() && folder.getMode() != Folder.READ_WRITE) { folder.close(true); } if (!folder.isOpen()) { folder.open(Folder.READ_WRITE); } for (Message message : messagez) { message.setFlag(Flags.Flag.SEEN, true); } logger.info("Marked email(s) as read : " + messagez.size()); return this; }
/** * 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; }
public CustomProperties getMessageProperties(Message message, String prefix, CustomProperties p) throws MessagingException, IOException { CustomProperties envVars = new CustomProperties(); String msgSubject = stringify(message.getSubject()); envVars.put(prefix + "subject", msgSubject); envVars.put(prefix + "from", stringify(message.getFrom())); envVars.put(prefix + "replyTo", stringify(message.getReplyTo())); envVars.put(prefix + "flags", stringify(message.getFlags())); envVars.put(prefix + "folder", stringify(message.getFolder())); envVars.put(prefix + "messageNumber", stringify(message.getMessageNumber())); envVars.put(prefix + "receivedDate", stringify(message.getReceivedDate())); envVars.put(prefix + "sentDate", stringify(message.getSentDate())); envVars.put(prefix + "headers", stringify(message.getAllHeaders())); envVars.put(prefix + "content", stringify(message)); envVars.put(prefix + "contentType", stringify(message.getContentType())); envVars.put(prefix + "recipients", stringify(message.getAllRecipients())); // add parameters from email content final CustomProperties properties = CustomProperties.read(getText(message)); envVars.putAll(properties); // envVars.put(prefix + "emailParams", stringify(properties.getMap(), "=", "&")); // add "jobTrigger" if (p.has(subjectContains)) { String subject = p.get(subjectContains); // normalise strings, find index, then get text after it int idx = msgSubject.toLowerCase().indexOf(subject.toLowerCase()); int beginIndex = idx + subject.length(); if (idx > -1 && beginIndex < msgSubject.length()) { envVars.put(prefix + "jobTrigger", msgSubject.substring(beginIndex).trim()); } } return envVars; }