5

What is the difference between these two from java mail perspective? I can see that 2 values are not same for a particular message. So what should I consider an IMAP message's unique id?

If I need to fetch message from an IMAP server corresponding to an unique id, should I use?

MessageIDTerm

or

IMAPFolder.getMessageByUID()
Anindya Chatterjee
  • 5,824
  • 13
  • 58
  • 82

1 Answers1

9

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. If I copy same email twice in a folder, each will have same headers having same message-id but will have a different UID.

Other major difference is,

  • UID's are assigned by a imap server
  • MessageId's are set by the email client.

So it is always better to rely on a UID to extract the email.

Refer : RFC - UID

Community
  • 1
  • 1
Sandeep Sukhija
  • 1,156
  • 16
  • 30
  • 2
    It is also much faster to request UIDs: it is the server's native index. Searching for Message-ID header may degenerate to a text search of all emails. – Max May 11 '16 at 14:04
  • true @Max, if you search for a Message-Id header, it will have to get the headers of each email and search within it. Its damn slow and may load the server if you have large number of mails in a folder. – Sandeep Sukhija May 11 '16 at 14:11
  • UID's of messages in a folder may be changed if mailserver is restarted, updated or on some cases. To detect the issue the UIDVALIDTY of folders should be checked. If you move a message between folders, the UID values will be changed, whereas MessageID's will be kept. – benchpresser May 13 '16 at 12:58
  • @benchpresser, UID's do not change on server restart. UID's get changed if server faces some data corruption triggers and it is a rare occurence these days. – Sandeep Sukhija May 13 '16 at 13:12
  • @benchpresser Yes true, the UID's will change if a mail is moved. UID's are as per folder, if a mail is moved from a folder to another, it will be treated as a new mail for a folder and will get assigned a next uid available for a folder. – Sandeep Sukhija May 13 '16 at 13:14