首页 > 解决方案 > 使用 EWS API 发送电子邮件方法获取 401 未经授权

问题描述

我对 EWS API 很陌生。现在我在使用 EWS 示例发送电子邮件时遇到问题。仍然收到错误:

401 未经授权。

这是我的示例代码:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
  service.Credentials = new WebCredentials("user1@contoso.com", "password");
  service.TraceEnabled = true;
  service.TraceFlags = TraceFlags.All;
  service.AutodiscoverUrl("user1@contoso.com", RedirectionUrlValidationCallback);
  EmailMessage email = new EmailMessage(service);
  email.ToRecipients.Add("user1@contoso.com");
  email.Subject = "HelloWorld";
  email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API");
  email.Send();

email.Send() 函数中的错误。对此有什么帮助吗?相关链接:https ://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications

我搜索了所有链接,但没有正确答案。

标签: outlookexchangewebservices

解决方案


在黑暗中拍摄,但很容易尝试:几年前,我遇到了无法解释的 401,发现一些网络帖子提到向服务对象添加一个空的 cookie 容器,即:

service.CookieContainer = new CookieContainer();

当时它似乎起到了作用,但不确定它是否仍然是一件事。


推荐阅读