skip to Main Content

We are using C# ASP.NET to read the mail in Outlook 365. These emails are then processed.

This code has been working for 2 years, but just started failing 8/24/2022.

Authentication is OAUTH, and after a Token is generated, this is the code we use to open a service connection:

var service = new ExchangeService();
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
service.Credentials = new OAuthCredentials(aToken);

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, EmailBox);
FolderId rootFolderId = new FolderId(WellKnownFolderName.Inbox);
ItemView view = new ItemView(500);

After establishing the service variable, we do a search of the inbox to pull back the mail currently in the inbox. For our scenario, we want all mail.

FindItemsResults<Item> findResults = service.FindItems(rootFolderId, view);

This is when the service returns a 500 error (An internal server error occurred. The operation failed.)

The error is sporadic. Sometimes it happens 8 times in a row before succeeding. I have mitigated errors on our system by looping until I get a success. But this is not an ideal solution.

After reading the mailbox, we also get the same error at 3 other functions:

  1. Binding the Email Message
  2. Marking the Email as Read
  3. Deleting the Email

I enabled Tracing. Here is the Request:

<Trace Tag="EwsRequest" Tid="7" Time="2022-08-26 11:46:03Z" Version="15.00.0847.030">
  <?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
      <t:RequestServerVersion Version="Exchange2013_SP1" />
      <t:ExchangeImpersonation>
        <t:ConnectingSID>
          <t:SmtpAddress>[email protected]</t:SmtpAddress>
        </t:ConnectingSID>
      </t:ExchangeImpersonation>
    </soap:Header>
    <soap:Body>
      <m:FindItem Traversal="Shallow">
        <m:ItemShape>
          <t:BaseShape>AllProperties</t:BaseShape>
        </m:ItemShape>
        <m:IndexedPageItemView MaxEntriesReturned="500" Offset="0" BasePoint="Beginning" />
        <m:ParentFolderIds>
          <t:DistinguishedFolderId Id="inbox" />
        </m:ParentFolderIds>
      </m:FindItem>
    </soap:Body>
  </soap:Envelope>
</Trace>

The trace didn’t give much in the response to go on.

<Trace Tag="EwsResponse" Tid="7" Time="2022-08-26 11:46:03Z" Version="15.00.0847.030">
  <?xml version="1.0" encoding="utf-8"?>
  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
      <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">*</Action>
    </s:Header>
    <s:Body>
      <s:Fault>
        <faultcode xmlns:a="http://schemas.microsoft.com/exchange/services/2006/types">a:ErrorInternalServerError</faultcode>
        <faultstring xml:lang="en-US">An internal server error occurred. The operation failed.</faultstring>
        <detail>
          <e:ResponseCode xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">ErrorInternalServerError</e:ResponseCode>
          <e:Message xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">An internal server error occurred. The operation failed.</e:Message>
        </detail>
      </s:Fault>
    </s:Body>
  </s:Envelope>
</Trace>

Our goal is to find the cause and correct, or report the issue to Microsoft in a way they can help resolve the issue on their end.

We are not opposed to updating our code if there is a newer method we should be using to read the mail.

I do not believe it is an issue in the OAUTH, as the error only ever returns when reading the mailbox, and it will eventually work if we keep hitting it with the request. I have to assume Microsoft changed something on their end we need to account for.

2

Answers


  1. Chosen as BEST ANSWER

    This may be a workaround until Microsoft resolves the underlying issue, but a comment in another post has us add this, which resolves the issues I've been seeing.

    service.HttpHeaders.Add("X-AnchorMailbox", EmailBox);
    

    Where EmailBox = the box I am impersonating ([email protected])

    I have not seen the issue in the last 2 hours after adding


  2. See the docs: https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth

    We have fixed this issue. Just do not use credentials, instead, use headers:

    1. Get token with scope – https://outlook.office365.com/EWS.AccessAsUser.All
    2. Add the token to headers:
    var service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
    service.setUrl(URI.create("https://outlook.office365.com/ews/exchange.asmx"));
    service.getHttpHeaders().put("Authorization", "Bearer " + token);
    

    That’s all, no any other headers, just valid token and correct service config.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search