Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I uniquely identify a Java Mail Message using IMAP?

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 ?

like image 982
Rakesh Waghela Avatar asked Jul 11 '12 15:07

Rakesh Waghela


People also ask

What is UID in mail?

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.

What is Java mail name the protocols used in Java mail and explain?

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.


3 Answers

Look at the UIDFolder interface, which exposes the IMAP UID capability.

like image 200
Bill Shannon Avatar answered Oct 19 '22 16:10

Bill Shannon


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
like image 43
Peter T. Avatar answered Oct 19 '22 15:10

Peter T.


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.

like image 1
A Paul Avatar answered Oct 19 '22 16:10

A Paul