1

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.

2
  • 3
    Only thing that stands out in your code is when you calling graphServiceClient while marking message as read, you do Me endpoint, while on retrieving it you call User[mailbox]. If sessions are different then Id of the message will point to different mailbox and it will fail there. Commented Sep 13, 2019 at 0:54
  • It is really not recommended to use .Result on an async method. You really should use await if it is possible. Especially if you are running this in an ASP.NET site. Commented Sep 17, 2019 at 0:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, we made this change in the code and it fixed some of our mailbox access, however, several other mailboxes needed to be rebuilt entirely in O365 before they would work.
Did you solved the problem for all mailboxes? I have same problem for some mailboxes and not other, very strange!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.