Dazz
Dazz

Reputation: 629

EWS has attachments but Attachment Collection is empty (one drive)

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

Upvotes: 1

Views: 1481

Answers (2)

moszti
moszti

Reputation: 86

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

Upvotes: 0

Glen Scales
Glen Scales

Reputation: 22032

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

Upvotes: 1

Related Questions