9

my code is:

 using (ImapClient client = new ImapClient())
 {
     // Connect to the server and authentication and then 
     var inbox = client.Inbox;
     inbox.Open(FolderAccess.ReadOnly);
     int messageCount = inbox.Count - 1;
     for (int i = messageCount; i > 0 ; i--)
     {
           var visitor = new HtmlPreviewVisitor();
           MimeMessage message = inbox.GetMessage(i);
           message.Accept(visitor);
           // how can get uid for this message
     }
 }

I wand to save uid . how can get uid for message ?

MimeMessage message =inbox.GetMessage(UniqueId.Parse(uid));
shahroz
  • 359
  • 1
  • 6
  • 17
  • What package is this using? `MimeMessage` doesn't appear to be standard C#, so it would be nice to add the `using` section so we know where the class comes from – Draken Apr 28 '16 at 09:52
  • add MimeKit ,MailKit and MailKit.Net.Imap. – shahroz Apr 28 '16 at 09:53
  • Looking in the class document, there doesn't appear to be anything of that type: http://www.mimekit.net/docs/html/T_MimeKit_MimeMessage.htm, the closest I can see is the property `MessageId`. Is that what you are looking for? If not, what do you need the `uid` for? – Draken Apr 28 '16 at 09:59
  • I wand save uid in place and use it for get content of message. MimeMessage message =inbox.GetMessage(UniqueId.Parse(uid)); – shahroz Apr 28 '16 at 10:03
  • I make list of message , then user select message from list.I want save uid to get this message from inbox. – shahroz Apr 28 '16 at 10:36
  • So you are OK with creating your own `UID`? If so, you could create a collection of `UID` and `MimeMessage` or create a class object that holds these properties. Then at point of creation of the message you would call `Guid.NewGuid();` to have a `UID` – Draken Apr 28 '16 at 10:38
  • is have way for it(make list message and user select it then we get message selected for user)? – shahroz Apr 28 '16 at 10:50

1 Answers1

18

The way to get the UID for a particular message using MailKit is to use the Fetch() method on the ImapFolder instance and pass it the MessageSummaryItem.UniqueId enum value.

Typically you'll want to get the UIDs of the messages in the folder before you fetch the actual message(s), like so:

// fetch some useful metadata about each message in the folder...
var items = folder.Fetch (0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.Size | MessageSummaryItems.Flags);

// iterate over all of the messages and fetch them by UID
foreach (var item in items) {
    var message = folder.GetMessage (item.UniqueId);

    Console.WriteLine ("The message is {0} bytes long", item.Size.Value);
    Console.WriteLine ("The message has the following flags set: {0}", item.Flags.Value);
}

The Flags includes things like Seen, Deleted, Answered, etc. The Flagged flag means that the message has been flagged as "important" by the user.

jstedfast
  • 35,744
  • 5
  • 97
  • 110