1

Is it possible to get attachment with EWS Managed API directly, without getting its containing EmailMessage first?

Something like:

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

I couldn't find any method for this.

Alex
  • 2,469
  • 3
  • 28
  • 61

1 Answers1

2

You can use the GetAttachments method of the ExchangeService Class https://msdn.microsoft.com/en-us/library/office/dn600509(v=exchg.80).aspx , this allows you to batch one or more GetAttachment requests together, for example:

     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");
            }
        }
    }
methon.dagger
  • 505
  • 9
  • 28
Glen Scales
  • 20,495
  • 1
  • 20
  • 23