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

filter email with attachment using mimekit/mailkit

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

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

遗憾的是,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