1

So I am trying to figure out how to get messages with UIDs

IMAPStore store = (IMAPStore) session.getStore("imaps");
store.connect();
IMAPFolder folder = (IMAPFolder) store.getFolder(FOLDER_NAME);
folder.open(Folder.READ_ONLY);

Then I want to search the folder:

Message unreadMessages[] = 
    folder.search(new FlagTerm( new Flags(Flags.Flag.SEEN), false));

But the messages in the array are returned without UIDs, so how do I pull them out later for processing without an ID to reference them by?

davesbrain
  • 606
  • 7
  • 12
  • Its worth noting that IMAPFolder implements UIDFolder [link](https://javamail.java.net/nonav/docs/api/com/sun/mail/imap/IMAPFolder.html) – davesbrain Aug 02 '16 at 14:29

1 Answers1

2

I'm assuming you're talking about IMAP UIDs and you know how IMAP UIDs work. The deleted answer explained this well. Folder.search gives you a bunch of Message objects. Using the UIDFolder.getUID method, you can iterate over each of those Message objects and get its corresponding UID. Using the Folder.fetch method, you can prefetch those UIDs with a single IMAP command so the iteration over each Message to get its UID happens locally. If you save the UIDs, you can later use the UIDFolder methods to get back the corresponding Message objects. Don't forget to check the UIDVALIDITY.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • Thanks, I was struggling because I didn't understand why the Message object didn't contain the UID – davesbrain Aug 02 '16 at 20:48
  • Because only IMAP messages have UIDs. The IMAP UID support is represented by the UIDFolder interface. I suppose we could've had a UIDMessage interface with a getUID method, but it seemed just as easy to put that single method on the UIDFolder method since the UIDs are all relative to the folder. – Bill Shannon Aug 03 '16 at 02:14