簡體   English   中英

使用 EWS 托管 API 和 C# 從郵箱中搜索附件

[英]Search attachments from a mailbox using EWS Managed API and C#

我想搜索名稱中包含某些關鍵字的郵箱中的所有附件。我正在使用 C# EWS 托管 API(2.2 版)執行此操作。 我可以使用Item.HasAttachment:true屬性訪問帶有附件的項目,並且代碼按預期工作。 但是處理時間很長。

目前的流程是: 1.從郵箱中獲取所有文件夾。 2.對於每個文件夾,搜索具有附件的項目(使用Item.HasAttachment:true searcFilter)。 3.檢查附件名稱是否包含關鍵字。

我需要知道是否有更好更快的方法來使用 EWS 訪問郵箱/文件夾中的附件。 有沒有辦法在文件夾級別對附件應用過濾器,而不是檢查每個郵件項目?

下面是用於按名稱關鍵字獲取附件的代碼片段

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

我建議您使用 QueryString 而不是 SearchFilter 這意味着您將進行內容索引搜索而不是文件夾限制,這要快得多。 例如

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

您可以使用 EWSEditor https://github.com/dseph/EwsEditor/releases輕松進行測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM