使用EWS Managed API 2.0立即从EWS检索附件

时间:2015-11-06 01:21:59

标签: c# exchangewebservices

我正在使用EWS来检索电子邮件,但是当我想要检索附件时,我必须为每个附件调用以下函数:

fileAttachment.Load();

每次我这样做,都会进入服务器。是否可以一次检索所有附件?此外,是否可以检索多个邮件的所有附件?

1 个答案:

答案 0 :(得分:1)

ExchangeService对象具有GetAttachments方法,该方法基本上允许您执行批处理GetAttachment请求。因此,如果您想要同时加载多个消息上的附件,您需要执行类似的操作(首先调用loadpropertiesforitems来执行批处理GetItem以获取AttachmentIds)

        FindItemsResults<Item> fItems = service.FindItems(WellKnownFolderName.Inbox,new ItemView(10));
        PropertySet psSet = new PropertySet(BasePropertySet.FirstClassProperties);
        service.LoadPropertiesForItems(fItems.Items, psSet);
        List<Attachment> atAttachmentsList = new List<Attachment>();
        foreach(Item ibItem in fItems.Items){
            foreach(Attachment at in ibItem.Attachments){
                atAttachmentsList.Add(at);
            }
        }
        ServiceResponseCollection<GetAttachmentResponse> gaResponses = service.GetAttachments(atAttachmentsList.ToArray(), BodyType.HTML, null);
        foreach (GetAttachmentResponse gaResp in gaResponses)
        {
            if (gaResp.Result == ServiceResult.Success)
            {
                if (gaResp.Attachment is FileAttachment)
                {
                    Console.WriteLine("File Attachment");
                }
                if (gaResp.Attachment is ItemAttachment)
                {
                    Console.WriteLine("Item Attachment");
                }
            }
        }

干杯 格伦