JavaMail: how to get new messages comparing with t

2020-07-10 06:26发布

问题:

I'm trying to get messages after a certain time-stamp, the way I've coded it was suggested by another programmer in this site:

GregorianCalendar date = new GregorianCalendar();
SearchTerm newer = new ReceivedDateTerm(ComparisonTerm.GT,date.getTime());
Message msgs[] = folder.search(newerThen);

The issue is that I get all the messages since the date, not the specific time. I was wondering if there is some work-around to emulate this. I mean, for an instance, if I want to get all the messages since today in the midday I would get those messages spicifically and not those ones received in today's morning.

Thanks in advance,

EDIT:

A new thought concerning to this: perhaps some date manipulation could do the job. I mean, comparing the minutes in the timestamp and filter programmatically those messages that don't fit the criteria. I know it's not the best way, but it could work.

PS: I'm using IMAP and trying to get mails from gmail, but I guess it should work no matter what the mail-server is.

回答1:

Unfortunately, no. In this case, the IMAP protocol is being used by the JavaMail classes, and IMAP's SEARCH command takes only dates, not times (see the SINCE and SENTSINCE criteria).



回答2:

You could use the setTime() method to query for some specific time.

Example:

setTime(timeInMilliseconds)


标签: imap javamail