IMAP Message in Java Mail is identified by it's relative position number which starts from 1.
refer, http://docs.oracle.com/javaee/1.4/api/javax/mail/Message.html#getMessageNumber()
Message number is a temporary details.
Is there a way to permanently uniquely identify a mail/message which accessing a mailbox via IMAP using Java Mail API which holds true across sessions ?
UID is the unique identification number of a email in a IMAP folder . Each mail in a folder is assigned a uid, it is you can say a index maintained by the mail folder. Whereas message-id is a header part of a email. To understand in a simple term, UID is a unique number which cannot be duplicated within a folder.
The JavaMail is an API that is used to compose, write and read electronic messages (emails). The JavaMail API provides protocol-independent and plateform-independent framework for sending and receiving mails. The javax. mail and javax.
Look at the UIDFolder interface, which exposes the IMAP UID capability.
You can get a unique identifier for a message using the following code as an example
Folder folder = imapStore.getFolder("INBOX"); // get reference for inbox folder
UIDFolder uf = (UIDFolder)folder; // cast folder to UIDFolder interface
folder.open(Folder.READ_ONLY); // open folder
Message messages[] = folder.getMessages(); // get all messages
Long messageId = uf.getUID(messages[0]); // get message Id of first message in the inbox
Adding this because I think this will help
If you want to loop over the messages and get uids the above code does not do got performance as you have to loop and get uid, easy way to get that with the messages is below
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
UIDFolder uf = (UIDFolder)emailFolder;
Message messages[] = emailFolder.getMessages();
FetchProfile profile = new FetchProfile();
//profile.add(FetchProfile.Item.ENVELOPE);
profile.add(FetchProfile.Item.FLAGS);
//profile.add(FetchProfile.Item.CONTENT_INFO);
profile.add(UIDFolder.FetchProfileItem.UID);
emailFolder.fetch(messages, profile);
this will get the messages with the uid
after this if you loop and do uf.getUID(messages[i]); you will get the uid but this time it will be faster.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With