How to determine the UID of a message in IMAP
I'm working in a mail client project using C#. I'm using both the POP and IMAP protocol to communicate with the server. The problem is than I can not figure out why when I want to get the UID for a message the result from the POP server and the IMAP server are different.
POP C: UIDL 1 S: +OK 1 UID2-1269789826
IMAP C: $ FETCH 1 (UID) S: * 1 FETCH (UID 2) S: $ OK Fetch completed.
Why the result for obtaining the UID is so different? In IMAP is another function for this? Any help is welcome. Thanks.
POP3 and IMAP are two distinct protocols. The UIDs used by these two protocols are not related and are not supposed to be the same at all. In fact, a POP3 UID is an arbitrary string while an IMAP UID is a 32-bit number (transmitted as string).
In theory, it is possible to write a mailserver that supports both IMAP and POP3 use the same UID for both protocols, but I'm not aware of any server that would actually do this. In practice, you have to treat POP3 UIDs and IMAP UIDs as unrelated values.
RFC 1939 (POP3): The unique-id of a message is an arbitrary server-determined string, consisting of one to 70 characters in the range 0x21 to 0x7E, which uniquely identifies a message within a maildrop and which persists across sessions.
RFC 3501 (IMAP): (Unique Identifier (UID) Message Attribute is) 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 or any subsequent mailbox with the same name forever.
Very important IMAP fact
This is what I learnt playing with GMail (I expect other systems to be the same):
- Sent a message to my inbox
- It has UID 45
- Move the message to junk
- It now has UID 5 (because UID is unique per mailbox)
- Move it back to inbox
- It now has UID 46
- Move it back to junk
- It now has UID 6
- Get the idea...
I moved the messages using the GMail.com UI
As far as I can tell UID is only useful in finding the latest emails in a folder. You can run the command Search("
UID 34:*) to get all messages in a folder with UID 34 or above.
But don't start using UID as a key to that message in a database or you'll end up with dupes.
I think I'm going to take the time received and hash it with the message contents to get a UID I can actually put into a database.