When I try sending email using the EWS API, I get the following error: (in message.Send();
)
The request failed. The remote server returned an error: (401) Unauthorized.
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…