简体   繁体   English

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

[英]filter email with attachment using mimekit/mailkit

is there a way to filter emails with attachments only? 有没有办法过滤带附件的电子邮件? I'm using this code 我正在使用此代码

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

Unfortunately, IMAP does not provide a search query term for checking if a message has an attachment, but what you can do is construct a search query with the other criteria that you want (much like you have already done), and then do: 遗憾的是,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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM