我有一个使用Aspose.Email的Windows服务,使用EWS调用Exchange365来读取邮箱文件夹列表,选择一个文件夹,然后读取邮件。我正在使用OAuth客户端凭据身份验证。
它在大多数时候都很好,但偶尔(大约1-2%的呼叫(我会遇到这样的错误:
ExchangeException: An internal server error occurred. The operation failed.
at #=zJwR5hYhASPjEG51KcFP5cfaNW_E9xKzbKNrDvkouRDML.#=zbEiudxU=(Exception #=zXBxojZA=)
at #=zJwR5hYhASPjEG51KcFP5cfaNW_E9xKzbKNrDvkouRDML.#=zJxs8Enk=()
at Aspose.Email.Clients.Exchange.WebService.EWSClient.#=zeKhM5WV7jZFJ(String #=zwl3m1OEGe_npeTCLKA==, ICredentials #=z8dcYagA=, WebProxy #=zPxR7k20=, #=zobHVK5XZznS2TuzCjFYVkWlb_06X8KyvTg== #=zuLDkIL8cyRRE, #=zLSBmP6s5p40Nd06_7HFvrX6K9S$RdU6i23eiqR0t6nn3VKSIig== #=zPNhWaZtYKy02YJZNn6uibe8=)
at Aspose.Email.Clients.Exchange.WebService.EWSClient.GetEWSClient(String mailboxUri, ICredentials credentials, WebProxy proxy)
at Aspose.Email.Clients.Exchange.WebService.EWSClient.GetEWSClient(String mailboxUri, ICredentials credentials)
at EmailTest.Program.GetFolders(OAuthNetworkCredential credentials) in C:DevSandboxEmailTestProgram.cs:line 72
at EmailTest.Program.<Main>d__0.MoveNext() in C:DevSandboxEmailTestProgram.cs:line 42
或
ExchangeException: An internal server error occurred. The operation failed.
at #=zRSjcm9RrA3a6zmmitFu34UW3nW4gV823KL6N1k9mXXhY.#=zqfYcQSUW7xkI(WebException #=zMMH$jH0=)
at #=zRSjcm9RrA3a6zmmitFu34UW3nW4gV823KL6N1k9mXXhY.GetResponse()
at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request)
at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at #=zKdOmVOFQs9ARtLG3Zlu_IvXd08LXFmI3_5D9s_gr4KsZjsevGw==.FindFolder(FindFolderType FindFolder1)
at #=zKdOmVOFQs9ARtLG3Zlu_ImWGLEjOliBGtOJU8_WNO6d9X7U9LuJoRwA=.#=zthDK7XA=(BaseFolderIdType #=zW0dJQvKwqFVf, String #=z7p_vI0c=)
at #=zKdOmVOFQs9ARtLG3Zlu_ImWGLEjOliBGtOJU8_WNO6d9X7U9LuJoRwA=.#=zthDK7XA=(String #=zxohf2Vezm7Vf, String #=z$vyJZib10Ez_, String #=z7p_vI0c=)
at #=zTAbjhrGUH9E0n8cOeUNrQiqgnbUhJNI_gUj7wuZ$HYDp.ListSubFolders(String #=zxohf2Vezm7Vf, String #=znaKuRg4Rx3vw)
at #=zTAbjhrGUH9E0n8cOeUNrQiqgnbUhJNI_gUj7wuZ$HYDp.ListSubFolders(String #=znaKuRg4Rx3vw)
at EmailTest.Program.GetFolders(OAuthNetworkCredential credentials) in C:DevSandboxEmailTestProgram.cs:line 74
at EmailTest.Program.<Main>d__0.MoveNext() in C:DevSandboxEmailTestProgram.cs:line 42
如果我稍后立即重试失败的调用,它会正常工作。因此,作为一种变通方法;重试";模式,但我不喜欢这样,我想知道为什么它失败了。出于调查目的,我制作了一个代码的精简版。
以下是我创建凭据的方式:
private static async Task<OAuthNetworkCredential> GetAccessTokenWithClientCredentials()
{
var app = ConfidentialClientApplicationBuilder
.Create(Settings.ClientId)
.WithTenantId(Settings.TenantId)
.WithClientSecret(Settings.ClientSecret)
.Build();
AuthenticationResult authResult =
await app.AcquireTokenForClient(new[] { Settings.Scopes }).ExecuteAsync().ConfigureAwait(false);
return new OAuthNetworkCredential(Settings.Username, authResult.AccessToken);
}
以下是我对文件夹的称呼:
private static string GetFolders(OAuthNetworkCredential credentials)
{
using (IEWSClient client = EWSClient.GetEWSClient(Settings.ExchangeWSUrl, credentials))
{
ExchangeFolderInfoCollection folders = client.ListSubFolders(client.MailboxInfo.RootUri);
return string.Join(", ", folders.Select(x => x.DisplayName));
}
}
这是使用Aspose.Email 22.8和Microsoft.Identity.Client 4.46.2。
ExchangeWSUrl为https://outlook.office365.com/ews/exchange.asmx
作用域为https://outlook.office.com/.default
如果你使用client_credentails流,你需要使用模拟,并且你应该将X-AnchorMailbox标头设置为你模拟的邮箱(我不使用apose,但他们的文档中有一个例子https://docs.aspose.com/email/net/utility-features/)。这会影响请求的路由方式,也就是说,邮箱经常在云中移动,所以可用性并不总是得到保证,如果邮箱被移动,你应该总是预计一些请求会失败。
更新:我根本没有更改我的代码或Aspose版本,也不再发生异常。
我的感觉是,微软在Exchange方面做了一些改变来解决这个问题,这是一个快乐的日子。