28

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.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
GwenGuts
  • 497
  • 1
  • 4
  • 12
  • 1
    In a situation where the EWS connection previously worked, and then isn't working, it may be something as simple as the password having expired, and needs to be changed. – RenniePet Nov 04 '17 at 08:44
  • have you find solution for this issue ? – Mohamme5d Apr 23 '22 at 18:26

3 Answers3

42

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

Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
  • Ours worked locally as 'domain\username', however external I had to split the domain and user name. – jtimperley Dec 16 '13 at 17:29
  • 1
    I actually had to switch my username to a fully qualified email address, ie. `first.last@contoso.com`, in order to get it to work. Specifying the AD domain or not (the third argument of the `WebCredential` constructor) made no difference for me. – Coxy Sep 15 '15 at 01:09
2

In my case, I needed to add to the EWS Virtual Directory under the IIS site the list of allowed URLs.

  1. Go to the IIS management, click the EWS node, under the Default Web Site, then double-click the Request Filtering.

  2. Go to the URL tab, and on the right, click Allow URL.

  3. Enter the URLs by which you will invoke the service, e.g. example.com/ews/ or 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).

TylerH
  • 20,799
  • 66
  • 75
  • 101
0

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.

exchangeService.Credentials = new WebCredentials("email", "app-pwd");
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
KSampath
  • 33
  • 5
  • for the individual email, I can do it. How about for all the emails within the organization? Scenario: User should be able to enter username and password and be able to retrieve emails in my application. – Avishekh Bharati May 04 '21 at 23:20