我想从名称中包含某些关键字的邮箱中搜索所有附件。我正在使用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);
}
}
}
}
}
}