C# Microsoft图形API错误:MailboxNotEnabledForRestAPI

C# Microsoft图形API错误:MailboxNotEnabledForRestAPI,c#,microsoft-graph-api,console-application,daemon,C#,Microsoft Graph Api,Console Application,Daemon,我正在使用c#创建一个守护程序应用程序,该应用程序使用Microsoft Graph API从组织中的特定outlook帐户发送邮件 我们使用客户端凭据流对Azure Active Directory进行身份验证。应用程序不需要用户登录,而是具有发送邮件的权限 我的exchange环境是本地环境,我们的IT经理成功地运行了混合部署步骤 下面是我的代码 using Microsoft.Graph; using Microsoft.Graph.Auth; using Microsoft.Identi

我正在使用c#创建一个守护程序应用程序,该应用程序使用Microsoft Graph API从组织中的特定outlook帐户发送邮件

我们使用客户端凭据流对Azure Active Directory进行身份验证。应用程序不需要用户登录,而是具有发送邮件的权限

我的exchange环境是本地环境,我们的IT经理成功地运行了混合部署步骤

下面是我的代码

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)
            {
               
            }
        }
    }
使用Microsoft.Graph;
使用Microsoft.Graph.Auth;
使用Microsoft.Identity.Client;
使用制度;
使用System.Collections.Generic;
使用System.Net.Http.Header;
使用System.Threading.Tasks;
专用静态异步任务RunAsync()
{
AuthenticationConfig=AuthenticationConfig.ReadFromJsonFile(“appsettings.json”);
IConfidentialClient应用程序;
app=secretentialClientApplicationBuilder.Create(config.ClientId)
.WithClientSecret(config.ClientSecret)
.WithAuthority(新Uri(配置权限))
.Build();
string[]scopes=新字符串[]{$“{config.ApiUrl}.default};
AuthenticationResult=null;
尝试
{
结果=等待app.AcquireTokenForClient(作用域)
.ExecuteAsync();
Console.ForegroundColor=ConsoleColor.Green;
Console.WriteLine(“获得代币”);
Console.ResetColor();
}
捕获(MsalServiceException ex)
}
}
如果(结果!=null)
{
ClientCredentialProvider authProvider=新的ClientCredentialProvider(应用程序);
GraphServiceClient graphClient=新的GraphServiceClient(authProvider);
var消息=新消息
{
Subject=“测试图”,
重要性=重要性。正常,
Body=新项目Body
{
ContentType=BodyType.Html,
Content=“正文在这里。”
},
ToRecipients=新列表()
{
新收件人
{
EmailAddress=新的EmailAddress
{
地址=”jack@example.com"
}
}
}
};
字符串emailaddress=”john@example.com";
尝试
{
等待graphClient.Users[emailaddress].SendMail(消息,SaveToSenItems:false)。请求().PostAsync();
}
捕获(ServiceException ex)
{
}
}
}
这成功地运行了几次,但随后由于MailboxNotEnabledForRestAPI错误而停止工作。 为了让它重新开始工作,我降低了一个nuget软件包的等级,它可以发送更多的电子邮件。然后我升级了软件包,它还能工作几次。 它似乎并不介意软件包在哪个版本上,只要我对它们做一点更改,它就会工作一段时间

这是一个在后台运行的应用程序,它可以检查特定的que以查找需要发送的电子邮件。可能有数百封电子邮件需要通过特定员工的邮箱与此应用程序一起发送


如何让应用程序在不中断的情况下继续发送所有邮件?即使我没有对应用程序的代码或exchange环境的设置进行任何更改,它也会崩溃。

各位,为什么这个问题已经解决了?我认为它有很多细节。这也发生在我身上,似乎是微软需要修复的一个bug。结束这个问题对任何人都没有帮助。类似的问题只是细节少了很多,没有代码,获得1票,没有结束。甚至有一个答案。这有什么用呢?你只是喜欢关闭和DV好的内容。我会尝试用MS Graph explorer或POSTMAN重新处理该问题,看看是否存在隔离该问题的问题。在此之前,我会检查我的exchange/o365管理员,如果一切正常,则不会因为您是否看到此问题而进行任何更改。@Dev我在Postman中也有相同的行为。我的exchange管理员说也没有任何更改