Microsoft Graph API Error: MailboxNotEnabledForRestAPI

Viewed 636

I am creating a daemon application with c# that uses the Microsoft Graph API to send mail from specific outlook accounts in my organization.

We use client credential flows to authenticate to Azure Active Directory. The application does not need a user to log in but rather has the permissions to send mail.

My exchange environment is on-premise and our IT manager successfully ran the steps for hybrid deployment.

Below is my Code

using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Threading.Tasks;


private static async Task RunAsync()
    {
        AuthenticationConfig config = AuthenticationConfig.ReadFromJsonFile("appsettings.json");
        IConfidentialClientApplication app;

            app = ConfidentialClientApplicationBuilder.Create(config.ClientId)
                .WithClientSecret(config.ClientSecret)
                .WithAuthority(new Uri(config.Authority))
                .Build();
      
        string[] scopes = new string[] { $"{config.ApiUrl}.default" }; 

        AuthenticationResult result = null;

        try
        {
            result = await app.AcquireTokenForClient(scopes)
                .ExecuteAsync();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Token acquired");
            Console.ResetColor();
        }
        catch (MsalServiceException ex) 
        }
        }

        if (result != null)
        {
            ClientCredentialProvider authProvider = new ClientCredentialProvider(app);
            GraphServiceClient graphClient = new GraphServiceClient(authProvider);
            
            var message = new Message
            {
                Subject = "Testing Graph",
                Importance = Importance.Normal,
                Body = new ItemBody
                {
                    ContentType = BodyType.Html,
                    Content = "Body text is here."
                },
                ToRecipients = new List<Recipient>()
                {
                    new Recipient
                    {
                        EmailAddress = new EmailAddress
                        {
                            Address = "jack@example.com"
                        }
                    }
                }
            };
            string emailaddress = "john@example.com";
            try
            {
                 await graphClient.Users[emailaddress].SendMail(message, SaveToSentItems: false).Request().PostAsync(); 
            }
            catch (ServiceException ex)
            {
               
            }
        }
    }

This successfully works for a couple of runs but then stops working with the error of MailboxNotEnabledForRestAPI. To get it to start working again I downgrade one of the nuget packages and it's able to send a few more emails. Then I upgrade the package and it works a few more times. It doesn't seem to mind which version the packages are on, as long as I make a change to them it works for a bit.

This is an application that runs in the background and checks a specific que for any emails that need to go out. There can be hundreds of emails that need to go out with this application thru a specific employee's mailbox.

How can I get the application to continue sending all mail forever without breaking? It breaks even though I didn't make any changes to the code of the application or the settings of the exchange environment.

0 Answers
Related