1
votes

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 (e.g. 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)
1
If it seems sporadic, you might try adding transient error handling. Or .. give us the full exception. - Cameron
I probably will fallback to a retry strategy, hadn't appreciated that this is such a formal pattern - thanks. It would have been nice to fix the core problem though. - hims987

1 Answers

0
votes

This issue can cause the two-step verification provide by Microsoft office 365. Better create a app password and pass that instead of outlook password.

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