How to determine the UID of a message in IMAP
Asked Answered
D

2

10

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

and

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.

Daytoday answered 30/3, 2010 at 7:41 Comment(0)
P
19

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.

Premier answered 30/3, 2010 at 19:31 Comment(2)
"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." I fully realize that in IMAP, the UIDVALIDITY can change, invalidating all UIDs, and UIDs SHOULD NOT change in IMAP (while in POP, UIDL MUST NOT change), but as far as implementation goes, if a server enforces the invariant that UIDs MUST NOT change, why do you feel that no server would actually do this? Curious if there are other reasons...Whip
I should have said "I am not aware of any server that actually does this". Sorry for the confusion! :-)Premier
S
11

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.

Scalp answered 21/12, 2013 at 4:28 Comment(2)
UID is only unique when combined with UIDVALIDITY and it's specific to a folder (IMAP protocol calls it a mailbox) (see tools.ietf.org/html/rfc3501#section-2.3.1.1 for details). However, even though UID/UIDVALIDITY combo solves the dupes, it is not guaranteed to stay intact - servers are permitted to change UIDVALIDITY, even though it's discouraged.Premier
Also, please note that Gmail does actually provide MSGIDs that are guaranteed to be unique and don't change. Use "Gmail unique message ID" (MSGID) extension to access this functionality: developers.google.com/gmail/imap_extensionsPremier

© 2022 - 2024 — McMap. All rights reserved.