In our internal webmail system, we'd like to attach notes and contacts to certain mails. In order to do this, we have to keep track of every mail on our IMAP server.

Unfortunately the IMAP standard doesn't enforce the uniqueness of the UID of a mail in a mailbox (just in subfolders). Is there any tool/IMAP server which generates UIDs which are truly unique? Or is there any other way how we can identify each mail? (the Message-ID header field is not unique as some mails do not contain such a field).

Additional resources: Unique ID in IMAP protocol - Limilabs.com

  • 1
    if it's only about incoming email, a custom MTA milter that adds UUID would be enough? – neutrinus Oct 26 '14 at 7:16

A tuple of (folder name, folder UIDVALIDITY, message UID) distinguishes a message from all others in an IMAP account. For any given message, that tuple can change between sessions, so you might want to track its Message-ID as a backup in case this happens.

Yes, it really is that annoying.

  • But RFC-2822 says that Message-ID SHOULD exist in a message which means that there can be no Message-ID at all – Romeno Apr 28 '18 at 5:35
  • 1
    That's right, @Romeno. IMAP is flawed and Message-ID only helps sometimes. – ʇsәɹoɈ Apr 28 '18 at 18:06

I don't know what you mean when you say UIDs are not unique in a mailbox, just in subfolders. By subfolders, do you mean anything other than INBOX? In any case, it sounds wrong. UIDs are in fact unique within each folder and it doesn't matter it it's INBOX or any other folder.

What can happen is that UIDs can change between sessions. Generally this happens if the IMAP server loses its indexes and has to rebuild them or if the mail is migrated from one IMAP server to another, etc... If this happens then you will of course lose all of the associations between emails and extra data (notes or contacts).

You can rely on this: a UID will never suddenly refer to a different message. Either it will remain valid and refer to the same message it always referred to, or else it will become invalid.

You might want to check out how your particular IMAP server generates and maintains UIDs. Different IMAP servers make different promises about the persistency of UID values. Even within one IMAP server, it may vary depending on which mailbox format is in use. For example, with Dovecot, expect less fragile UIDs with mdbox than with mbox or Maildir because the metadata are more tightly integrated with the actual emails with mdbox.

I suggest using UIDs to refer to messages on the IMAP server, with Message-ID as a backup. Message-ID is not as good because duplicates are possible and (for most IMAP servers) searching by Message-ID might be slower, but it's better than losing track of the messages completely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.