protected boolean areMessagesInReverseOrder(Folder inbox) throws MessagingException { try { Message msgFirst = inbox.getMessage(1); Message msgLast = inbox.getMessage(inbox.getMessageCount()); Date firstMessageDate = getDateToDetermineOrder(msgFirst); Date lastMessageDate = getDateToDetermineOrder(msgLast); return firstMessageDate != null && lastMessageDate != null && firstMessageDate.after(lastMessageDate); } catch (Exception e) { return false; } }
public Collection<Integer> getFirstTimeMessages(Folder inbox, String context) throws Exception { log.debug(String.format("handling first mailcheck for %s", context)); Collection<Integer> messages = new ArrayList<Integer>(); // we need to get 5 most recent messages in this case if (inbox.getMessageCount() > 0) { if (areMessagesInReverseOrder(inbox)) { log.debug(String.format("order is reverse chronological for %s", context)); for (int i = 0; i < 5; i++) { messages.add(i + 1); } } else { log.debug(String.format("order is chronological for %s", context)); for (int messageNumber = inbox.getMessageCount(); messageNumber > 0 && messageNumber > inbox.getMessageCount() - 5; messageNumber--) { messages.add(messageNumber); } } } return messages; }