1

I want to fetch mails that are received in the last 1 hour. I checked it here -

java imap fetch messages since a date

and tried the following -

DateTime current = new DateTime();
DateTime oneHourLess = current.minusHours(1);

SearchTerm olderThan = new ReceivedDateTerm(ComparisonTerm.LT, current.toDate());
SearchTerm newerThan = new ReceivedDateTerm(ComparisonTerm.GT, oneHourLess.toDate());
SearchTerm andTerm = new AndTerm(olderThan, newerThan);
Message totalMess[] = inbox.search(andTerm);

But i get no mails since the ReceivedDateTerm does not take time into consideration.

Is there any way out how to do this?

Community
  • 1
  • 1
Dan
  • 801
  • 2
  • 14
  • 29

2 Answers2

7

The IMAP protocol only provides resolution to the day, no time.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
4

We had a similar requirement and what we did was to use the SearchTerm which will get applied for the date. You can then use getReceivedDate() to filter out older contents that came for the day.

Also, for me, getReceivedDate() was coming in as null. I had to use getSentDate() and SentDateTerm

rajesh
  • 3,247
  • 5
  • 31
  • 56