【问题标题】:MailKit IMAP search by UID and download individual MIME partsMailKit IMAP 按 UID 搜索并下载单个 MIME 部分
【发布时间】:2017-10-28 07:43:17
【问题描述】:

我已经能够检索到我检查的最后一封邮件之后的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 上进行过滤数据然后只下载电子邮件的一部分

【问题讨论】:

    标签: c# imap mailkit


    【解决方案1】:

    以下是如何混合两者:

    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 方法有一个接受 UniqueID 的 IList 的重载

    【讨论】:

    • 你能分享你的代码吗?完整的代码?我正在为我的大学项目设计一个电子邮件客户端,需要一些帮助来从服务器获取 IMAP 电子邮件并在 webbrowser 控件中显示。
    猜你喜欢
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 2011-01-05
    相关资源
    最近更新 更多