如何在mailkit中获取uid?

时间:2016-04-28 09:13:18

标签: c# mailkit

我的代码是:

 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
     }
 }

我想拯救你。怎么能得到消息的消息?

MimeMessage message =inbox.GetMessage(UniqueId.Parse(uid));

1 个答案:

答案 0 :(得分:8)

使用MailKit获取特定邮件的UID的方法是使用Fetch()实例上的ImapFolder方法并将MessageSummaryItem.UniqueId枚举值传递给它。

通常,您希望在获取实际消息之前获取文件夹中的消息的UID,如下所示:

// 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);
}

Flags包含SeenDeletedAnswered等内容。Flagged标记表示该邮件已被标记为&#34 ;重要"由用户。