0
votes

I can't save an EWS email message in Office 365 outlook.

Hello. I try to add an email message by using Microsoft.Exchange.WebServices lib in C#. I create a new 'Microsoft.Exchange.WebServices.Data.EmailMessage', which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope>
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2007_SP1" />
    <t:TimeZoneContext>
      <t:TimeZoneDefinition Id="UTC" />
    </t:TimeZoneContext>
    <t:ExchangeImpersonation>
      <t:ConnectingSID>
        <t:PrimarySmtpAddress>[email protected]</t:PrimarySmtpAddress>
      </t:ConnectingSID>
    </t:ExchangeImpersonation>
  </soap:Header>
  <soap:Body>
    <m:CreateItem MessageDisposition="SaveOnly">
    <m:SavedItemFolderId>
      <t:FolderId Id="AQMkAGFjMjFlOTJlLWI3YjYtNDZlMS04MTRkLTYyYWECMTg2MmIzAC4AAAMRJLfC0jQnTqZjTdO9e1KoAQCygVW3rMc4SbvynFv3xnH6AAACAQwAAAA=" />
    </m:SavedItemFolderId>
    <m:Items>
      <t:Message>
        <t:Subject>SubjectSample</t:Subject>
      <t:Body BodyType="HTML">BodySample</t:Body>
      <t:ExtendedProperty>
        <t:ExtendedFieldURI PropertyTag="57" PropertyType="SystemTime" />
        <t:Value>2017-02-17T16:37:38.000</t:Value>
      </t:ExtendedProperty>
      <t:ExtendedProperty>
        <t:ExtendedFieldURI PropertyTag="3590" PropertyType="SystemTime" />
        <t:Value>2017-02-17T16:37:38.000</t:Value>
      </t:ExtendedProperty>
      <t:ExtendedProperty>
        <t:ExtendedFieldURI PropertyTag="3591" PropertyType="Integer" />
        <t:Value>1</t:Value>
      </t:ExtendedProperty>
      <t:From>
        <t:Mailbox>
        <t:Name>support</t:Name>
        <t:EmailAddress>[email protected]</t:EmailAddress>
        </t:Mailbox>
      </t:From>
      <t:IsRead>true</t:IsRead>
      </t:Message>
    </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

When i try to Save this EWS message, i get an error 'Microsoft.Exchange.WebServices.Data.ServiceResponseException' with response code: 'ErrorInvalidExchangeImpersonationHeaderData' and message: 'An ExchangeImpersonation SOAP header must contain a user principal name, user SID, or primary SMTP address'. I use ServiceAccount authentication.

A few days ago it worked fine.

1
The error is telling you that the emailaddress being used in PrimarySmtpAddress isn't the primary email address of the account. if it worked a few days ago and has stopped working mostly likely the PrimarySMTPAddress has been changed.Glen Scales
PrimarySmtpAddress didn't change.Yuri B

1 Answers

0
votes

I haven't worked on Email message but as per msdn link read Anchor Mailbox and with appointment fetch experience, try with passing Anchor Mailbox header.

Sample code is beow

ImpersonatedUserId impersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, strAttendeeEmail);
                                    _service.ImpersonatedUserId = impersonatedUserId;
                                    if (!_service.HttpHeaders.ContainsKey("X-AnchorMailbox"))
                                        _service.HttpHeaders.Add("X-AnchorMailbox", strAttendeeEmail);
                                    else
                                        _service.HttpHeaders["X-AnchorMailbox"] = strAttendeeEmail;