Unique ID in IMAP protocol

RFC 3501 specification says:

“2.3.1.1. Unique Identifier (UID) Message Attribute

A 32-bit value assigned to each message, which when used with the unique identifier validity value (see below) forms a 64-bit value that MUST NOT refer to any other message in the mailbox (folder) or any subsequent mailbox (folder) with the same name forever. Unique identifiers are assigned in a strictly ascending fashion”

“The unique identifier of a message MUST NOT change during the session, and SHOULD NOT change between sessions.”

“Any change of unique identifiers between sessions MUST be detectable using the UIDVALIDITY mechanism”

“The unique identifier validity value is sent in a UIDVALIDITY response code in an OK untagged response at mailbox (folder) selection time.”

Any change of unique identifiers between session causes the change of FolderStatus.UIDValidity:

using(Imap client = new Imap())
{
    client.Connect("imap.example.org");
    FolderStatus folderStatus = client.Select("Inbox");
    Console.WriteLine(
        "Folder UIDValidity: " + folderStatus.UIDValidity);
    client.Close();
}
Using client As New Imap()
    client.Connect("imap.example.org")
    Dim folderStatus As FolderStatus = client.Select("Inbox")
    Console.WriteLine( _
        "Folder UIDValidity: " + folderStatus.UIDValidity)
    client.Close()
End Using

Uniqueness

Unfortunately: “There is no assurance that the UIDVALIDITY values of two mailboxes (folders) be different, so the UIDVALIDITY in no way identifies a mailbox (folder).”

This means that:

  • UID of the email may be not unique on the server (2 messages in different folders may have same UID)
  • FolderStatus.UIDValidity + UID may be not unique on the server (2 folders may have same UIDValidity)
  • To identify a message across all folders you need 3 variables: UID, folder name and FolderStatus.UIDValidity.

UIDs across sessions

  • UIDs are ‘supposed’ to be stable across sessions, and never change, and always increase in value.
  • You need to check the FolderStatus.UIDValidity when you select the folder. If the FolderStatus.UIDValidity number hasn’t changed, then the UIDs are still valid across sessions.

Other

  • UIDs are assigned in ascending fashion – higher the value newer the message.

Tags:   

Questions?

Consider using our Q&A forum for asking questions.

2 Responses to “Unique ID in IMAP protocol”

  1. Users Says:

    Hi, I checked in Gmail and yahoo… I never see the changes of UIDVALIDITY value. When i create it, was assigned as 10. after multiple session workout and new and existing email changes, still it has 10. May i know what is its actual Purpose

  2. Limilabs support Says:

    @Users,

    Most common reason for such change is when folder was deleted and then created with the same name – UIDVALIDITY must be different in such case to allow client to recognize that this a different folder.
    If you want details you’ll need to look in RFC3501 e.g. 2.3.1.1.

    In most modern servers UIDVALIDITY changes rarely. However client that remembers message UIDs between sessions must deal with such change and act accordingly.