C# Exchange Web Service API 和 401 未授权异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13517323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:52:02  来源:igfitidea点击:

Exchange Web Service API and 401 unauthorized exception

c#exchangewebserviceshttp-status-code-401exchange-server-2007unauthorized

提问by GwenGuts

When I try sending email using the EWS API, I get the following error: (in message.Send();)

当我尝试使用 EWS API 发送电子邮件时,出现以下错误:(在message.Send();

The request failed. The remote server returned an error: (401) Unauthorized.

请求失败。远程服务器返回错误:(401) 未经授权。

My code is the following:

我的代码如下:

ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

//WebService Uri
try
{
    exchangeService.Url = new Uri("https://exchangeserver/ews/exchange.asmx");
}
catch (Exception ex)
{
    throw new Exception(string.Format("WebService Uri:" + ex));
}

//Credentials
try
{
    exchangeService.Credentials = new WebCredentials("user@domain", "pwd", "domain");
}
catch (Exception ex)
{
    throw new Exception(string.Format("Credentials:" +  ex));
}

//Send a mail
try
{
    EmailMessage message = new EmailMessage(exchangeService);
    message.Subject = "Test";
    message.Body = "Test";
    message.ToRecipients.Add("destination@domain");
    message.Save();
    message.Send();
}
catch (Exception ex)
{
    throw ex;
}

I read other posts on this site concerning this issue but they couldn't resolve my issue.

我在本网站上阅读了有关此问题的其他帖子,但他们无法解决我的问题。

采纳答案by Carlos Landeras

Try changing this:

尝试改变这个:

 exchangeService.Credentials = new WebCredentials("user@domain", "pwd", "domain");

into this:

进入这个:

 exchangeService.Credentials = new WebCredentials("user", "pwd", "domain");

Sometime the Login credentials depends on how Exchange/Active Directory it's configured. It could be user@domain or domain\user

有时,登录凭据取决于 Exchange/Active Directory 的配置方式。它可以是 user@domain 或 domain\user

回答by KirilStankov

It took me a lot of time to find a solution for the same issue. In my case, I needed to add to the EWS Virtual Directory under the IIS site the list of allowed URL's. Go to the IIS management, click the EWS node, under the Default Web Site, then double-click the Request Filtering. Go to the URL tab, and on the right, click Allow URL. Enter the url's by which you will invoke the service, e.g. example.com/ews/ or server.example.com/ews/

我花了很多时间才找到相同问题的解决方案。就我而言,我需要将允许的 URL 列表添加到 IIS 站点下的 EWS 虚拟目录。进入 IIS 管理,点击 EWS 节点,在 Default Web Site 下,然后双击 Request Filtering。转到 URL 选项卡,然后在右侧单击允许 URL。输入您将用来调用服务的 URL,例如 example.com/ews/ 或 server.example.com/ews/

In addition, related to similar issues, I needed to add all hosts (*) to the winrm trusted host (by default it had only the local IP listed).

此外,与类似问题相关,我需要将所有主机 (*) 添加到 winrm 受信任主机(默认情况下,它仅列出本地 IP)。

HTH.

哈。

回答by KSampath

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

此问题可能导致 Microsoft Office 365 提供的双向身份验证。最好创建应用程序密码并传递该密码,而不是 Outlook 密码。

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