Search code examples
c#microsoft-graph-apimicrosoft-graph-sdksmicrosoft-graph-mail

Microsoft Graph API - cannot access individual message (exception)


We have some code which has been running successfully for months now, and all of a sudden yesterday, it is failing.

Basically, we have a process which logs into mailboxes via the Graph API C# SDK (v1.12.0), gets the unread messages, iterates through each one, does some processing, and then tries to mark each message as "read".

Here is the relevant code:

var graphserviceClient = new GraphServiceClient(_metaData.GraphBaseURL,
                  new DelegateAuthenticationProvider(
                  (requestMessage) =>
                  {
                      requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
                      requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                      requestMessage.Headers.Add("Prefer", "outlook.body-content-type='text'");

                      return Task.FromResult(0);
                  }));

                var unreadMails = graphserviceClient
                                .Users[_metaData.MailBox]
                                .MailFolders
                                .Inbox
                                .Messages
                                .Request()
                                .Filter("isRead eq false")
                                .Top(1000)
                                .Select("Body, Subject")
                                .GetAsync()
                                .Result;

                emailRetrievalCount = unreadMails.Count();

                // Determine if any results were returned
                if (unreadMails.Count > 0)
                {
                    // Create loop to process each item in the newResult object
                    foreach (var unreadMail in unreadMails)
                    {
                        // Set the isRead flag of the message to true
                        var readMail = graphserviceClient
                                       .Me
                                       .Messages[unreadMail.Id] //BREAKS HERE!!!
                                       .Request()
                                       .Select("IsRead")
                                       .UpdateAsync(new Message { IsRead = true })
                                       .Result;
                     }
                }

Exception Message:

Microsoft.Graph.ServiceException: Code: ErrorInternalServerError\r\nMessage: An internal server error occurred. The operation failed., Cannot open mailbox.

We checked permissions on the mailbox or account, nothing has changed. Also, it doesn't seem to be a permissions issue since we can get a token, log in, and get the list of messages fine. It's just when we try to get a specific message to update the "Read" status, it fails. Also, we tried updating the SDK to the latest version v1.19.0, but the same issue occurs.


Solution

  • I was able to confirm that there was a service behavior change that now prevents access to an email message from a different mailbox. This was not correct behavior and has been fixed. By changing your call to access .Users[_metaData.MailBox].Messages[unreadMail.Id] the problem should be resolved.