5

after some googling without results about this I'll ask my questions:

What is the Mailkit.UniqueId?

In the metadata of the struct is simply reads

Represents a unique identifier for messages in a MailKit.IMailFolder

1) Is it unique only for let's say the Inbox but then the Bin or Sent might have the same number again (I doubt it from the messages I have retrieved so far)

2) Is it unique for the entire imap account?

3) Is it an id produced by mailkit or by the Imap Server?

4) What controls the incrementation of this Id to ensure it's unique.

5) What are the main differences between this UniqueID and the MessageID ?

penCsharpener
  • 409
  • 7
  • 15
  • If u wanna do something with a particular message,i'ld rather suggest u to go with `MessageId` which i found unique.Dunno much about the unique id,it just seems to be count of the email...E.g.,The email that is retrieved will have it's uniqueId set to 1 , the 2nd email will have 2 and so on –  Mar 06 '18 at 15:54
  • I noticed that too. UniqueID just seems to number through the messages in the INBOX. However, the MessageID is not 100% reliable either because although it should be there it can be missing or have a value that would easily colide in a busy Inbox. For my intends and purposes it would be enough if I could count on UniqueID being unique for that imap account among all MailFolders. – penCsharpener Mar 06 '18 at 16:03
  • MessageId is unique,at least it looks so.....Hey...i have a problem [here](https://stackoverflow.com/questions/49130306/retrieve-emails-in-descending-reverse-order-using-mailkit-c-sharp?noredirect=1#comment85274554_49130306) ...can u help me out ? –  Mar 06 '18 at 16:06
  • ok, I think I found some answers after all http://wesmorgan.blogspot.de/2012/07/understanding-email-headers-part-ii.html that's that MessageID *should* be unique but it's not enforced. in this version of Imap https://tools.ietf.org/html/rfc3501 it says that the UniqueID together with the Validation id is unique for the mail account – penCsharpener Mar 06 '18 at 16:10
  • any help here : https://stackoverflow.com/questions/49130306/retrieve-emails-in-descending-reverse-order-using-mailkit-c-sharp?noredirect=1#comment85274554_49130306 –  Mar 06 '18 at 16:12

1 Answers1

8

A MailKit UniqueId represents an IMAP UID. A UID as defined by IMAP is a numeric value that is unique per folder.

This means that a message in the Inbox can have the same UID as a different message in the Sent folder.

As per the comment discussion below your question, yes, UIDs are sequentially assigned which means that as long as messages are never deleted from a folder, the first message in the folder will have a UID of 1. The second message will have a UID of 2, etc.

This will not be the case, however, once messages start getting deleted from the folder.

For example, let's say we create a new IMAP folder and append 10 messages.

Their UIDs will be: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

If I then go and delete messages: 1, 3, 5, 7, and 9, and then look at the messages still remaining in the folder, they will have the following UIDs: 2, 4, 6, 8, and 10.

phuzi
  • 12,078
  • 3
  • 26
  • 50
jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Thanks. As to your last example; when a new msg would arrive after the deletions, it would get uid 11, right? – penCsharpener Mar 06 '18 at 17:13
  • So when I save the combination of IMAP account, uid and mailfolder I've got a global unique identifier as well (one that is not a long string like Message-ID), right? – penCsharpener Mar 06 '18 at 17:21
  • @jstedfast Referring your example, if i delete 1, 3, 5, 7, 9 and 10, the next UID will be 9 or 11? – Rizwan Ali Mar 06 '19 at 11:25
  • 3
    The next uid will be 11. Uids are never reused. – jstedfast Mar 06 '19 at 11:47
  • How to convert long/int to Mailkit.UniqueId in c#? I am trying to delete an email from a folder but getting a "cannot implicitly convert type long to Mailkit.UniqueId" type casting error. **folder.Expunge(new UniqueId[] { 2 })** – Prashant Girase Nov 03 '22 at 06:34
  • 1
    @PrashantGirase you need to use `new UniqueId[] { new UniqueId(2) }` – jstedfast Nov 07 '22 at 12:59