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

Retrieve attachments from EWS at once using EWS Managed API 2.0

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

fileAttachment.Load();

每次我这样做,它都会转到服务器。是否可以一次检索所有附件?另外,是否可以检索多个邮件项目的所有附件?

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

干杯 格伦