Instead of retrieving all messages, you should try using server-side search. This works using the javax.mail.Folder search method. You may have to write your own SearchTerm based on the criteria of Message.getReceivedDate ().
If server side search is not working, you can try using the fetch profile, i.e. use inbox.fetch instead of inbox.getMessages () (message [] msgs, FetchProfile fp). The javadoc for fetch says: Clients use this method to indicate that the specified elements are necessary for an array of a given range. Implementations are expected to extract these elements for a given range of messages in an efficient manner. Note that this method is just a hint of an implementation for pre-fetching the necessary elements.
diciu
source share