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

Gwe*_*uts 25 c# exchange-server-2007 unauthorized exchangewebservices http-status-code-401

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

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

我的代码如下:

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;
}
Run Code Online (Sandbox Code Playgroud)

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

Car*_*ras 38

尝试改变这个:

 exchangeService.Credentials = new WebCredentials("user@domain", "pwd", "domain");
Run Code Online (Sandbox Code Playgroud)

进入这个:

 exchangeService.Credentials = new WebCredentials("user", "pwd", "domain");
Run Code Online (Sandbox Code Playgroud)

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