使用mimekit / mailkit过滤带有附件的电子邮件

时间:2018-12-17 09:10:13

标签: c# mailkit mimekit

有没有办法只过滤带有附件的电子邮件? 我正在使用此代码

using (var client = new ImapClient())
       {
         client.Connect(IMAPServer, IMAPport, IMAPSSL);
         client.AuthenticationMechanisms.Remove("XOAUTH2");
         client.Authenticate(User, Password);
         var inbox = client.Inbox;
         inbox.Open(FolderAccess.ReadOnly);
         //filter email with attachments only
           var results = inbox.Search(SearchQuery.NotSeen.And(SearchQuery.NotDeleted));
  }

1 个答案:

答案 0 :(得分:1)

不幸的是,IMAP不提供用于检查邮件是否带有附件的搜索查询词,但是您 可以做的是使用您想要的其他条件构造搜索查询(非常像您已经完成),然后执行以下操作:

var results = inbox.Search(SearchQuery.NotSeen.And(SearchQuery.NotDeleted));
var items = MessageSummaryItems.BodyStructure | MessageSummaryItems.UniqueId;
var matched = new UniqueIdSet ();

foreach (var message in inbox.Fetch (results, items)) {
    if (message.BodyParts.Any (x => x.IsAttachment))
        matched.Add (message.UniqueId);
}

// `matched` now contains a list of UIDs of the messages that have attachments
// and also fit your other search criteria