Patrik Björklund
Patrik Björklund

Reputation: 1237

Can't get attachments from EWS for fowarded emails in an Outlook addin in the desktop version of Outlook 2016

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

Upvotes: 2

Views: 1542

Answers (2)

Jthorpe
Jthorpe

Reputation: 10167

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()

Upvotes: 1

Patrik Bj&#246;rklund
Patrik Bj&#246;rklund

Reputation: 1237

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"

Upvotes: 4

Related Questions