如何使用EWS托管API通过id获取附件?

时间:2016-07-07 15:06:48

标签: c# exchange-server exchangewebservices

是否可以直接获得EWS托管API的附件,而不首先获取其包含的EmailMessage?

类似的东西:

FileAttachment attach = FileAttachment.Bind(ewsService, attachId);

我找不到任何方法。

1 个答案:

答案 0 :(得分:2)

您可以使用ExchangeService类https://msdn.microsoft.com/en-us/library/office/dn600509(v=exchg.80).aspx的GetAttachments方法,这允许您一起批处理一个或多个GetAttachment请求,例如:

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