JavaMail: how to get new messages comparing with time-stamps
Asked Answered
M

2

7

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.

Mahmud answered 10/1, 2011 at 19:24 Comment(1)
If you indent your code with 4 spaces, SO will format it as code rather than as a wrappable text block.Transship
T
10

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).

Transship answered 10/1, 2011 at 19:30 Comment(3)
Thanks dkarp. Further speaking, is there any way to emulate the behavior of the mail agents about getting new messages? I mean, if I want to get new messages only (and not to download them more than once), how could I do?Mahmud
There are a few ways to do it. You can remember the highest UID in the folder, then do an ((UIDFolder) folder).getMessagesByUID(highuid + 1, UIDFolder.LASTUID). You can do a folder.search(new FlagTerm(new Flags(Flags.Flag.RECENT), true)) to get all messages added since the last IMAP client connected to the folder, though this won't work if other IMAP clients are active.Transship
Just an addition: You can call getUIDNext() on folder to get the next UID. Then you don't need to add + 1. Besides, you should store the IMAP UIDVALIDITY value and compare it to folder.getUIDValidity() to make sure that you can still rely on the stored highest UID. Check out this IMAP protocol section: datatracker.ietf.org/doc/html/rfc3501#section-2.3.1.1Agio
S
1

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

Example:

setTime(timeInMilliseconds)
Softener answered 15/11, 2011 at 20:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.