c# - Exchange Web Service API和401未经授权的例外

时间:2016-11-09 17:11:24

标签: c# exchangewebservices

以下EWS调用偶尔会生成401错误(1000封电子邮件中的1封)。

  
    

emailMessage.Load(属性集);

         

emailMessage.Move(WellKnownFolderName.DeletedItems);

  

关于问题是什么的任何想法?电子邮件驻留在InBox&受到许多规则扫描(由于驻留在InBox中),但规则不会从InBox中删除它们。稍后对同一电子邮件的重新处理将成功。凭证或propertySet没有问题,它只有静态引用。好像,emailMessage虽然从service.FindItems()中检索,但具有暂时的权限。我在EWS日志中没有看到任何相应的错误。

为了澄清,应用于收件箱的规则正在移动其他电子邮件。严格来说,我不知道ES(+消息实验室)在将电子邮件放入收件箱之前是否应用了规则(例如跨度检查等)。如果在插入后应用,我可以理解可能有两个进程针对InBox,可能同时进行。如果是这样,我不知道其含义可能是什么。这都是猜测工作 - 我不知道这是否与我的问题有关。但是,异常表明401来自EWS响应,即使我在EWS日志中找不到相同的内容。

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 个答案:

答案 0 :(得分:0)

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

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