【问题标题】:Search attachments from a mailbox using EWS Managed API and C#使用 EWS 托管 API 和 C# 从邮箱中搜索附件
【发布时间】:2020-03-01 10:30:18
【问题描述】:

我想搜索名称中包含某些关键字的邮箱中的所有附件。我正在使用 C# EWS 托管 API(2.2 版)执行此操作。 我可以使用 Item.HasAttachment:true 属性访问带有附件的项目,并且代码按预期工作。但是处理时间很长。

目前的处理流程是: 1.从邮箱中获取所有文件夹。 2.对于每个文件夹,搜索具有附件的项目(使用Item.HasAttachment:true searcFilter)。 3.检查附件名称是否包含关键字。

我需要知道是否有更好更快的方法来使用 EWS 访问邮箱/文件夹中的附件。有没有办法在文件夹级别对附件应用过滤器,而不是检查每个邮件项目?

下面是代码 sn-p 用于通过 name 关键字获取附件

SearchFilter searchFilter = new SearchFilter.IsEqualTo(ItemSchema.HasAttachments, true); //SearchFilter for finding item with attachments FindItemsResults<Item> searchResults = null; FileAttachment fileAttachmentobj = null; ItemAttachment itemAttachmentobj = null; for (var j = 0; j < folder.Count; j++) //Looping for all the folders in a mailbox { for (int i = 0; i < strAttachNameKeyword.Length; i++) //Looping for keywords to be searched { searchResults = service.FindItems(folder[j].Id, searchFilter, view); if (searchResults.TotalCount > 0) { service.LoadPropertiesForItems(searchResults, new PropertySet(BasePropertySet.IdOnly, ItemSchema.HasAttachments)); foreach (Item item in searchResults) //Processing each item in SearchResults { item.Load(); foreach (Attachment attachmentObj in item.Attachments) //for each attachment in an item { //attachmentObj.Load(); fileAttachmentobj = attachmentObj as FileAttachment; itemAttachmentobj = attachmentObj as ItemAttachment; if (fileAttachmentobj != null && (fileAttachmentobj.Name.Contains(strAttachNameKeyword[i]))) { //fileAttachmentobj.Load(); Console.WriteLine(fileAttachmentobj.Name); Console.WriteLine(fileAttachmentobj.Size); Console.WriteLine(fileAttachmentobj.Id); } } } } } }

【问题讨论】:

    标签: c# exchange-server exchangewebservices searchfiltercollection


    【解决方案1】:

    我建议您使用 QueryString 而不是 SearchFilter,这意味着您将进行内容索引搜索,而不是更快的文件夹限制。例如

    FindItemsResults fiItems = service.FindItems(QueryFolder, "Attachment:blah.pdf", iv);
    

    如果您右键单击文件夹->搜索项目并选择 AQS 单选按钮,您可以使用 EWSEditor https://github.com/dseph/EwsEditor/releases 轻松测试它,它具有用于 AQS/KQL 查询的简单界面

    【讨论】:

    • 我也尝试过使用 SearchQuery,但它的运行速度甚至比 SearchFilter 还要慢。如前所述,我需要搜索名称中包含某些关键字的附件。queryString Attachment:Keyword 也在附件的正文中搜索。我可以限制它不在附件正文中搜索吗?仅按附件名称?
    • attachmentnames:Sydney 在 Office365 docs.microsoft.com/en-us/microsoft-365/compliance/… 上工作(我认为这是最近添加的,但如果你有 OnPrem 值得一试)
    猜你喜欢
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多