【发布时间】: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