问题描述
我正在使用c#创建一个守护程序应用程序,该应用程序使用Microsoft Graph API从组织中的特定Outlook帐户发送邮件。
我们使用客户端凭据流对Azure Active Directory进行身份验证。该应用程序不需要用户登录,而是具有发送邮件的权限。
我的交换环境为本地部署,我们的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 = "[email protected]"
}
}
}
};
string emailaddress = "[email protected]";
try
{
await graphClient.Users[emailaddress].SendMail(message,SavetoSentItems: false).Request().PostAsync();
}
catch (ServiceException ex)
{
}
}
}
这可以成功运行几次,但随后由于出现MailBoxNotEnabledForRestAPI错误而停止工作。 为了使它重新开始工作,我将其中一个nuget软件包降级了,它可以发送更多电子邮件。然后,我对该软件包进行升级,并且可以再运行几次。 似乎并不介意这些软件包所使用的版本,只要我对其进行更改,便可以正常工作了。
这是一个在后台运行的应用程序,用于检查特定队列是否有需要发送的电子邮件。该应用程序可能需要通过特定员工的邮箱发送数百封电子邮件。
如何使应用程序永久继续发送所有邮件而不会中断?即使我没有对应用程序的代码或交换环境的设置进行任何更改,它也会中断。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)