[使用EWS托管API和C#从邮箱搜索附件

问题描述 投票:0回答:1

我想从名称中包含某些关键字的邮箱中搜索所有附件。我正在使用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);
                             }
                        }
                    }
                }
            }                       
         }
c# exchange-server exchangewebservices searchfiltercollection
1个回答
0
投票
我建议您使用QueryString而不是SearchFilter,这意味着您将进行内容索引搜索,而不是文件夹限制,这要快得多。例如
© www.soinside.com 2019 - 2024. All rights reserved.