java imap fetch messages since a date

Chaitan picture Chaitan · May 15, 2009 · Viewed 21.7k times · Source

I am writing/learning to fetch email using java from an IMAP folder using javax.mail package. I was successfully able to retrieve the last n messages in a Folder, however I am looking to build an example to retrieve messages since a specified date. Any examples?

Answer

Nick Stuart picture Nick Stuart · May 15, 2009

You could also use the SearchTerm classes in the java mail package.

SearchTerm olderThan = new ReceivedDateTerm(ComparisonTerm.LT, someFutureDate);
SearchTerm newerThan = new ReceivedDateTerm(ComparisonTerm.GT, somePastDate);
SearchTerm andTerm = new AndTerm(olderThan, newerThan);
inbox.search(andTerm);

Some combination of the above should prove to be a better way to get dates within a certain range.