MailKit IMAP 按 UID 搜索并下载单个 MIME 部分

MailKit IMAP search by UID and download individual MIME parts

我已经能够检索到我检查的最后一条消息之后的 UID 列表,因此我可以从 IMAP 服务器获取 只有新消息:

using(var client = new ImapClient())
{
   //client authentication code...

   var inbox = client.Inbox;
   inbox.Open(FolderAccess.ReadOnly);

   //msgIdx contains the UID of the last message I've checked,
   // so I can retrieve all the new messages after this one.
    var range = new UniqueIdRange(new UniqueId((uint) msgIdx), UniqueId.MaxValue);


    IList<UniqueId> uids = inbox.Search(range, SearchQuery.All);
    foreach(var uid in uids)
    {
       //With the following instruction I download the whole message...
       var message = inbox.GetMessage(uid);
       LblMessageLog.Text += uid + " Subject:" + message.Subject + " []<br/>";
    }
    client.Disconnect(true);
}

问题是我不想下载整封邮件,而是只下载一个特定的附件,因为我知道它会一直存在于所有邮件中。

MailKit 网站上有一个示例,但它与我上面的搜索冲突:

foreach (var summary in inbox.Fetch (0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure)) {
    if (summary.Body is BodyPartMultipart) {
        var multipart = (BodyPartMultipart) summary.Body;

        var attachment = multipart.BodyParts.OfType<BodyPartBasic> ().FirstOrDefault (x => x.FileName == "cert.xml");
        if (attachment != null) {
            // this will download *just* the attachment
            var part = inbox.GetBodyPart (summary.UniqueId, attachment);
        }
    }
}

如何在 UID 上执行 过滤数据 然后 仅下载电子邮件消息的一部分 两个过程?

以下是混合两者的方法:

var range = new UniqueIdRange(new UniqueId((uint) msgIdx),    UniqueId.MaxValue);

var items = inbox.Fetch(uids, MessageSummaryItems.UniqueId | MessageSummaryItems.Envelope);
foreach(var item in items)
{
    LblMessage.Text += item.UniqueId + " Subject:" + item.Envelope.Subject + "<br/>";
}

Fetch 方法有一个重载接受 UniqueIDs 的 IList