Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 924
Emanuel Avatar asked Mar 30 '10 07:03

Emanuel


People also ask

What is UID in IMAP?

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.

Is IMAP UID unique?

Internally, each IMAP message is assigned a unique ID (UID).

What is UID in mail?

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.


2 Answers

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.

like image 168
Lukas Pokorny Avatar answered Oct 01 '22 05:10

Lukas Pokorny


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.

like image 28
Simon_Weaver Avatar answered Oct 01 '22 03:10

Simon_Weaver