我们使用 C# ASP.NET 读取 Outlook 365 中的邮件。然后处理这些电子邮件。
此代码已经运行了 2 年,但从 2022 年 8 月 24 日开始失败。
认证是OAUTH,生成Token后,这是我们用来打开服务连接的代码:
var service = new ExchangeService();
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
service.Credentials = new OAuthCredentials(aToken);
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, EmailBox);
FolderId rootFolderId = new FolderId(WellKnownFolderName.Inbox);
ItemView view = new ItemView(500);
建立服务变量后,我们对收件箱进行搜索以拉回收件箱中当前的邮件。对于我们的场景,我们需要所有邮件。
FindItemsResults<Item> findResults = service.FindItems(rootFolderId, view);
这是服务返回 500 错误(发生内部服务器错误。操作失败。)
错误是偶发的。有时连续发生8次才成功。我通过循环减少了系统上的错误,直到成功为止。但这不是一个理想的解决方案。
读取邮箱后,我们在其他 3 个函数中也遇到了同样的错误:
我启用了跟踪。这是请求:
<Trace Tag="EwsRequest" Tid="7" Time="2022-08-26 11:46:03Z" Version="15.00.0847.030">
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2013_SP1" />
<t:ExchangeImpersonation>
<t:ConnectingSID>
<t:SmtpAddress>[email protected]</t:SmtpAddress>
</t:ConnectingSID>
</t:ExchangeImpersonation>
</soap:Header>
<soap:Body>
<m:FindItem Traversal="Shallow">
<m:ItemShape>
<t:BaseShape>AllProperties</t:BaseShape>
</m:ItemShape>
<m:IndexedPageItemView MaxEntriesReturned="500" Offset="0" BasePoint="Beginning" />
<m:ParentFolderIds>
<t:DistinguishedFolderId Id="inbox" />
</m:ParentFolderIds>
</m:FindItem>
</soap:Body>
</soap:Envelope>
</Trace>
跟踪没有给出太多继续的响应。
<Trace Tag="EwsResponse" Tid="7" Time="2022-08-26 11:46:03Z" Version="15.00.0847.030">
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">*</Action>
</s:Header>
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/exchange/services/2006/types">a:ErrorInternalServerError</faultcode>
<faultstring xml:lang="en-US">An internal server error occurred. The operation failed.</faultstring>
<detail>
<e:ResponseCode xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">ErrorInternalServerError</e:ResponseCode>
<e:Message xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">An internal server error occurred. The operation failed.</e:Message>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
</Trace>
我们的目标是找到原因并纠正,或者以 Microsoft 能够帮助解决问题的方式向 Microsoft 报告问题。
如果我们应该使用更新的方法来阅读邮件,我们并不反对更新我们的代码。
我不认为这是 OAUTH 中的问题,因为错误只会在读取邮箱时返回,如果我们继续使用请求,它最终会起作用。我必须假设微软改变了一些我们需要考虑的事情。
在 Microsoft 解决根本问题之前,这可能是一种解决方法,但另一篇文章中的评论让我们添加了此内容,这解决了我遇到的问题。
service.HttpHeaders.Add("X-AnchorMailbox", EmailBox);
其中 EmailBox = 我冒充的邮箱([电子邮件受保护])
添加后的过去 2 小时内我还没有看到该问题
我们已经解决了这个问题。只是不要使用
credentials
,而是使用标题:
https://outlook.office365.com/EWS.AccessAsUser.All
var service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.setUrl(URI.create("https://outlook.office365.com/ews/exchange.asmx"));
service.getHttpHeaders().put("Authorization", "Bearer " + token);
仅此而已,没有任何其他标头,只有有效的令牌和正确的服务配置。
@戴夫谢谢你!我被困在这个问题上24小时了!显然,最近(再次)交换查找文件夹发生了变化,这是我能够解决它的唯一方法