简体   繁体   中英

c# - Exchange Web Service API and 401 unauthorized exception

The following EWS calls sporadically generate 401 errors (1 in 1000 emails) processed.

emailMessage.Load(propertySet);

emailMessage.Move(WellKnownFolderName.DeletedItems);

Any ideas as to what the problem is? The emails reside with the InBox & are subject to a number of rule scans (by virtue of residing in the InBox), but the rules are not removing them from the InBox. A reprocess of the same email at a later time will be successful. There is no issue with credentials or the propertySet, which just has static references. It's as if, the emailMessage, though retrieved from the service.FindItems() has transitory permissions. I don't see any corresponding errors within the EWS logs.

To clarify, rules applied to inbox are moving OTHER emails. Strictly I don't know if the rules (eg span checks etc) are applied by ES (+message labs) before it puts the emails into the Inbox. If applied after insertion I can understand there may be 2 processes working against the InBox, maybe concurrently. If so I don't know what the implications might be. This is all guess work - I don't know if this has anything to do with my problem. However, the exception is indicating the 401 arises from the EWS response, even though I have not found the same in the EWS logs.

Error: Microsoft.Exchange.WebServices.Data.ServiceRequestException: The request failed. The remote server returned an error: (401) Unauthorized. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse()
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
   --- End of inner exception stack trace ---
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request)
   at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
   at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalLoadPropertiesForItems(IEnumerable`1 items, PropertySet propertySet, ServiceErrorHandling errorHandling)
   at Microsoft.Exchange.WebServices.Data.Item.InternalLoad(PropertySet propertySet)

Microsoft.Exchange.WebServices.Data.ServiceRequestException: The request failed. The remote server returned an error: (401) Unauthorized. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse()
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
   --- End of inner exception stack trace ---
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request)
   at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
   at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalMoveItems(IEnumerable`1 itemIds, FolderId destinationFolderId, Nullable`1 returnNewItemIds, ServiceErrorHandling errorHandling)
   at Microsoft.Exchange.WebServices.Data.ExchangeService.MoveItem(ItemId itemId, FolderId destinationFolderId)
   at Microsoft.Exchange.WebServices.Data.Item.Move(FolderId destinationFolderId)
   at Microsoft.Exchange.WebServices.Data.Item.Move(WellKnownFolderName destinationFolderName)

此问题可能导致Microsoft Office 365提供的两步验证。更好地创建应用程序密码并传递该密码而不是Outlook密码。

exchangeService.Credentials = new WebCredentials("email", "app-pwd");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM