public boolean isMessageTooOld(Account account, Message message, String context) throws MessagingException { if (message.getSentDate() == null) { log.warn( String.format("we have a message with no sent date for %s, allowing message", context)); return false; } else if (account.getRegister_time() == null) { log.warn( String.format( "we are process an account with no register time. this behavior is not understood yet %s, we will accept this message", context)); return false; } else { boolean messageTooOld = (System.currentTimeMillis() - message.getSentDate().getTime()) > 1000l * 60 * 60 * 24 * emailDaysCutoff; if (messageTooOld) { log.warn( String.format( "msgNum=%d, message is too old, sentDate=%s, discarding, for %s", message.getMessageNumber(), message.getSentDate(), context)); } return messageTooOld; } }
public Date getMessageDate(Message message) throws MessagingException { Date messageDate = message.getReceivedDate(); if (message.getReceivedDate() == null) { if (message.getSentDate() != null) { messageDate = message.getSentDate(); } } return messageDate; }
/** Returns the date the message was sent (or received if the sent date is null. */ public String getDate() throws MessagingException { Date date; SimpleDateFormat df = new SimpleDateFormat("EE M/d/yy"); if ((date = message.getSentDate()) != null) return (df.format(date)); else if ((date = message.getReceivedDate()) != null) return (df.format(date)); else return ""; }
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; }
/** * Deliveres notification to the user that an email has been dropped. Typically due to size * restriction * * @param account * @param messageNumber * @param message * @throws Exception */ public void saveMessageDroppedNotification( Account account, int messageNumber, Message message, int reportedSize) throws Exception { MessageParser parser = new MessageParser(); Email email = new Email(); email.setSubject( "Your email"); // ReceiverUtilsTools.subSubject(parser.parseMsgSubject(message), // subjectSize)); email.setFrom(parser.parseMsgAddress(message, "FROM", false)); email.setTo(ReceiverUtilsTools.subAddress(parser.parseMsgAddress(message, "TO", true))); email.setCc(ReceiverUtilsTools.subAddress(parser.parseMsgAddress(message, "CC", true))); email.setBcc(ReceiverUtilsTools.subAddress(parser.parseMsgAddress(message, "BCC", true))); email.setMaildate(ReceiverUtilsTools.dateToStr(message.getSentDate())); email.setStatus("0"); email.setUserId(account.getUser_id()); email.setMessage_type("EMAIL"); Body body = new Body(); String droppedMessage = getEmailDroppedMessage(account, getMessageDate(message), reportedSize, email.getFrom()); body.setData(droppedMessage.getBytes()); email.setBodySize(droppedMessage.length()); int saveStatus = DALDominator.newSaveMail(account, email, body); if (log.isDebugEnabled()) log.debug( String.format( "[%s] msgNum=%d, saving completed for dropped message with status %s", account.getName(), messageNumber, saveStatus)); }
public static void dumpEnvelope(Message m) throws Exception { pr("This is the message envelope"); pr("---------------------------"); Address[] a; // FROM if ((a = m.getFrom()) != null) { for (int j = 0; j < a.length; j++) pr("FROM: " + a[j].toString()); } // TO if ((a = m.getRecipients(Message.RecipientType.TO)) != null) { for (int j = 0; j < a.length; j++) pr("TO: " + a[j].toString()); } // SUBJECT pr("SUBJECT: " + m.getSubject()); // DATE Date d = m.getSentDate(); pr("SendDate: " + (d != null ? d.toString() : "UNKNOWN")); // FLAGS Flags flags = m.getFlags(); StringBuffer sb = new StringBuffer(); Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags boolean first = true; for (int i = 0; i < sf.length; i++) { String s; Flags.Flag f = sf[i]; if (f == Flags.Flag.ANSWERED) s = "\\Answered"; else if (f == Flags.Flag.DELETED) s = "\\Deleted"; else if (f == Flags.Flag.DRAFT) s = "\\Draft"; else if (f == Flags.Flag.FLAGGED) s = "\\Flagged"; else if (f == Flags.Flag.RECENT) s = "\\Recent"; else if (f == Flags.Flag.SEEN) s = "\\Seen"; else continue; // skip it if (first) first = false; else sb.append(' '); sb.append(s); } String[] uf = flags.getUserFlags(); // get the user flag strings for (int i = 0; i < uf.length; i++) { if (first) first = false; else sb.append(' '); sb.append(uf[i]); } pr("FLAGS: " + sb.toString()); // X-MAILER String[] hdrs = m.getHeader("X-Mailer"); if (hdrs != null) pr("X-Mailer: " + hdrs[0]); else pr("X-Mailer NOT available"); }
protected Message getFullyLoad(Message message, Folder folder, String context) throws MessagingException { try { ((GoogleVoiceFolder) folder).loadFully((GoogleVoiceMessage) message); } catch (Exception e) { throw new MessagingException("Exception getting googlevoice message body " + e.getMessage()); } Message wrappedMessage = new MimeMessage((MimeMessage) message); wrappedMessage.setSentDate(message.getSentDate()); return wrappedMessage; }
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; }
/** * checks message sent date againsts a predefined email days cutoff * * @param message * @param migratedAccount * @return * @throws javax.mail.MessagingException */ public boolean isMessageTooOld(Account account, Message message, String context) throws MessagingException { if (message.getSentDate() == null) { log.warn( String.format("we have a message with no sent date for %s, allowing message", context)); return false; } // This should handle the first login case, we want to take up to x days worth of old emails and // not be rejected // by the Too Old check if (account.getLast_received_date() == null) { log.warn( String.format( "we are process an account with no register time. this behavior is not understood yet %s, we will accept this message", context)); return (System.currentTimeMillis() - message.getSentDate().getTime()) > 1000l * 60 * 60 * 24 * emailDaysCutoff; } return isOlder(message.getSentDate(), account.getLast_received_date()); }
/** Method for checking if the message has a sent date field. */ public boolean hasSentDate() throws MessagingException { return (message.getSentDate() != null); }
/** Returns the sent date field. */ public String getSentDate() throws MessagingException { if (hasSentDate()) return (message.getSentDate().toString()); else return ""; }
public boolean processMessage( Account account, Message message, int messageNumber, String messageId, Folder folder, Set<String> storeMessageIds, String storeBucket, String context, StopWatch watch, FolderHelper folderHelper) throws Exception { boolean result = false; long start = System.nanoTime(); try { StopWatchUtils.newTask( watch, String.format("msgNum=%s, storeCheck", messageNumber), context, log); // Log.debug(log4j, "msgNum=%d, checking if messageId=%s is known for %s", messageNumber, // messageId, context); // Log.debug(log4j, "msgNum=%d, checking if messageId=%s is too old or too big or sent for // %s", messageNumber, messageId, context); StopWatchUtils.newTask( watch, String.format("msgNum=%s, tooOld-tooBig-check", messageNumber), context, log); if (isMessageTooOld(account, message, context)) { log.warn( String.format( "msgNum=%d, messageId=%s, message is too old, sentDate=%s, discarding, for %s", messageNumber, messageId, message.getSentDate(), context)); // this message is too old and needs to be ignored } else { message = getFullyLoad(message, folder, context); int messageSize = (folder instanceof POP3Folder) ? ((POP3Folder) folder).getMessageSize(messageNumber) : getMessageSize(message, account); if (messageSize > maximumMessageSize) { log.warn( String.format( "msgNum=%d, messageId=%s, message is too big %d bytes, discarding for %s", messageNumber, messageId, messageSize, context)); saveMessageDroppedNotification(account, messageNumber, message, messageSize); } else { if (log.isDebugEnabled()) log.debug( String.format( "msgNum=%d, messageId=%s, message is new for %s", messageNumber, messageId, context)); try { new EmailSaveProcess(account, watch, context) .process(message, messageNumber, messageId); } catch (Throwable e) { log.fatal( String.format( "msgNum=%d, messageId=%s, subject=%s message saving failed for %s", messageNumber, messageId, message.getSubject(), context), e); StopWatchUtils.newTask( watch, String.format("msgNum=%s, removeFromStore", messageNumber), context, log); } result = true; } } } finally { if (log.isDebugEnabled()) log.debug( String.format( "msgNum=%d, checked if messageId=%s is known in %dms for %s", messageNumber, messageId, (System.nanoTime() - start) / 1000000, context)); } return result; }
public static void dumpPart(Part p) throws Exception { if (p instanceof Message) { Message m = (Message) p; Address[] a; // FROM if ((a = m.getFrom()) != null) { for (int j = 0; j < a.length; j++) System.out.println("FROM: " + a[j].toString()); } // TO if ((a = m.getRecipients(Message.RecipientType.TO)) != null) { for (int j = 0; j < a.length; j++) System.out.println("TO: " + a[j].toString()); } // SUBJECT System.out.println("SUBJECT: " + m.getSubject()); // DATE Date d = m.getSentDate(); System.out.println("SendDate: " + (d != null ? d.toLocaleString() : "UNKNOWN")); // FLAGS: Flags flags = m.getFlags(); StringBuffer sb = new StringBuffer(); Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags boolean first = true; for (int i = 0; i < sf.length; i++) { String s; Flags.Flag f = sf[i]; if (f == Flags.Flag.ANSWERED) s = "\\Answered"; else if (f == Flags.Flag.DELETED) s = "\\Deleted"; else if (f == Flags.Flag.DRAFT) s = "\\Draft"; else if (f == Flags.Flag.FLAGGED) s = "\\Flagged"; else if (f == Flags.Flag.RECENT) s = "\\Recent"; else if (f == Flags.Flag.SEEN) s = "\\Seen"; else continue; // skip it if (first) first = false; else sb.append(' '); sb.append(s); } String[] uf = flags.getUserFlags(); // get the user flag strings for (int i = 0; i < uf.length; i++) { if (first) first = false; else sb.append(' '); sb.append(uf[i]); } System.out.println("FLAGS = " + sb.toString()); } System.out.println("CONTENT-TYPE: " + p.getContentType()); /* Dump input stream InputStream is = ((MimeMessage)m).getInputStream(); int c; while ((c = is.read()) != -1) System.out.write(c); */ Object o = p.getContent(); if (o instanceof String) { System.out.println("This is a String"); System.out.println((String) o); } else if (o instanceof Multipart) { System.out.println("This is a Multipart"); Multipart mp = (Multipart) o; int count = mp.getCount(); for (int i = 0; i < count; i++) dumpPart(mp.getBodyPart(i)); } else if (o instanceof InputStream) { System.out.println("This is just an input stream"); InputStream is = (InputStream) o; int c; while ((c = is.read()) != -1) System.out.write(c); } }
private Date getDateToDetermineOrder(Message message) throws MessagingException { return message instanceof IMAPMessage ? message.getReceivedDate() : message.getSentDate(); }