0
votes

when the email is sent from OWA browser with attachments. A link is created for attachments using one drive/sharepoint. when i tried to read the mail item from EWS the attachment collection is empty but hasAttachments flag is set to true. How do i access these attachments from EWS ?

EmailMessage message =  EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));

message.Attachments is empty
2
You need to show the code your using, reference attachments which is what you talking about requires that you set the requested server version to at least 2016 but you should also show the code you using the get that attachments for this to be a good question. - Glen Scales
I added code of reading attachments. I didn't see any examples of reading reference attachments. can i just read them like regular attachments? - Kumar Garapati
I've added a reference attachment sample, but something doesn't sound right you should still be getting something in the collection. if you look at the message with a MAPI editor like MFCMapi or OutlookSpy do you see any attachment objects? - Glen Scales

2 Answers

1
votes

Here is simple example of dealing with reference attachment for this to work you need to be using the latest version of the EWS Managed API from github (the nuget version won't have the correct classes).

        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2016);
        service.Credentials = new OAuthCredentials(Token);
        service.AutodiscoverUrl("[email protected]",adAutoDiscoCallBack);
        FindItemsResults<Item> fiResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(1));

        PropertySet psPropset = new PropertySet(BasePropertySet.IdOnly);
        psPropset.Add(ItemSchema.Attachments);

        EmailMessage Message = (EmailMessage)fiResults.Items[0];
        Message.Load(psPropset);
        foreach(Attachment Attachment in Message.Attachments)
        {
            if(Attachment is ReferenceAttachment referenceAttachment)
            {
                using (var client = new WebClient())
                {
                    client.Headers[HttpRequestHeader.Authorization] = "Bearer " + Token;
                    client.DownloadFile(referenceAttachment.AttachLongPathName, referenceAttachment.Name);
                }
            }
        }
0
votes

It looks like this is an Exchange bug. I was able to reproduce it in OutlookSpy as well. While IMessage was showing the attachment, EWS didn't return it. OWA is using EWS in the background...

For detailed answer see: Attachments with the ReferenceAttachment type ( ATTACH_BY_WEB_REF) from O365 mailbox are not returned using EWS