I’m wondering how message numbers in the JavaMail API work.
Are they always sorted by time?
What I’m trying to do is avoid refetching headers on a gmail folder.
Let’s say I have a folder with 20,000 messages, that I process today.
Tomorrow, there’s a 100 new messages, and I would prefer not to fetch all
20,000 headers again. Can I just ask for msg nums. 20,001 – 20,100?
I assume this would work in the normal case where later messages are added
to folders. But how about the case that a user manually inserts something
from an earlier time period into the folder using a client like Thunderbird?
[s]
It’s the server that controls how message numbers work. In essentially all cases, message numbers correspond to the order in which message are added to the mailbox.
In your Inbox, for example, this is the order in which messages are received. Your email client might display the messages in the order in which they were sent, which can be quite different.
Messages might be removed from the list, but messages are never inserted before an existing message. If you’re looking for new message, you have to handle the case where an existing message was deleted, changing the message numbers.
See the JavaMail UIDFolder interface, and the IMAP spec on which it was based. If you keep track of the last UID you’ve seen, you’ll only need to look at messages with UIDs larger than that. There’s lots more details, but that’s a start. Read the docs and let me know if you have questions.