2
votes

I have a problem with attachment id's of attachments in forwarded emails/emails in a thread.

When fetching attachments from the "source/original" email (both inline and normal attachments) I can successfully retrieve the content of the attachment from the EWS webservice by asking for attachment id's from Office.context.mailbox.item.attachments

When I try to get the same attachments from a fowarded version of the email I get "The specified attachment Id is invalid.ErrorInvalidAttachmentId0" for every attachment in the email. If I forward an email and add an extra attachment to the email before sending I get get the content of the attachment just for the "extra" attachment, not for any of the original attachments.

The error only occurs with the Outlook desktop client. (version 16.0.6366.2062). The problem does not exist in OWA when using chrome or internet explorer.

This is the code my API uses to call EWS.

string getAttachmentRequest =
    @"<?xml version=""1.0"" encoding=""utf-8""?>
    <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
    xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
    xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""
    xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
    <soap:Header>
    <t:RequestServerVersion Version=""Exchange2013"" />
    </soap:Header>
        <soap:Body>
        <GetAttachment xmlns=""http://schemas.microsoft.com/exchange/services/2006/messages""
        xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">
            <AttachmentShape/>
            <AttachmentIds>
            <t:AttachmentId Id=""{0}""/>
            </AttachmentIds>
        </GetAttachment>
        </soap:Body>
    </soap:Envelope>";
getAttachmentRequest = String.Format(getAttachmentRequest, attachmentId);

// Prepare a web request object.
HttpWebRequest webRequest = WebRequest.CreateHttp(ewsUrl);
webRequest.Headers.Add("Authorization", string.Format("Bearer {0}", authToken));
webRequest.PreAuthenticate = true;
webRequest.AllowAutoRedirect = false;
webRequest.Method = "POST";
webRequest.ContentType = "text/xml; charset=utf-8";

// Construct the SOAP message for the GetAttchment operation.
byte[] bodyBytes = System.Text.Encoding.UTF8.GetBytes(getAttachmentRequest);
webRequest.ContentLength = bodyBytes.Length;

Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(bodyBytes, 0, bodyBytes.Length);
requestStream.Close();

// Make the request to the Exchange server and get the response.
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
2
Can you get them client side using makeEwsRequestAsync? Or do a GetAttachments EWS request from the server?lgaud
When the addin is running in OWA in Internet Explorer/Chrome I can get all the attachments correctly. When I run the exact same addin in Outlook 2016, against the exact same email, it somehow gets invalid attachment id's. I have actually not yet saved the attachment id's generated by browser and compared them to the ones generated inside the restricted ie in outlook. But then again, don't know what I would do with that information. (Will try with the clientside makeEwsRequestAsync function, but I would still like to know why this wont work)Patrik Björklund
Just remembered that I have tried that already. It's not allowed. "Note that the server-side code should access EWS by directly creating EWS requests. You can’t use the Mailbox.makeEws­RequestAsync method to access the GetAttachment operation." msdn.microsoft.com/en-us/magazine/dn201750.aspxPatrik Björklund

2 Answers

4
votes

Andrew Salamatov answered this question on yammer:

"It's a bug that we have in Outlook with the way we calculate the id in this specific scenario. The workaround would be to make an EWS GetItem call and get the attachment id that way. So if the id you got from item.attachments[i].id doesn't work, you could fallback and make an EWS call"

1
votes

I came across this same issue when our EWS servers were upgraded to 2016, and I was previously downloading attached emails using the EWS Managed API in PowerShell via:

# load the email items.
$fiItems = $exchService.FindItems( $Inbox.Id, $email_filter, $ivItemView)  

# create the Attachment properties object
$attachment_PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet(
        [Microsoft.Exchange.WebServices.Data.ItemSchema]::Attachments)  

# LOAD THE ATTACHMENT AS AN EWS ITEM
$attached_email = $email_from_server.attachments[0]
$attached_email.load($attachment_PropertySet)

Apparently with the new EWS server the $attachment_PropertySet parameter causes an error, and the fix is simply to remove it, i.e.:

$attached_email.load()