0

I'm very new to EWS API. Now I've a problem when I Send Email using EWS sample. Still getting the error:

401 unauthorized.

Here is my sample code:

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();

The error in the email.Send() function. Any help for this? Related link:https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications

I searched all links but no correct answer.

JYDev
  • 1
  • 401 is pretty straight, either your credential is wrong or EWS was not configured properly on your Exchange Server. – wannadream Oct 30 '18 at 14:37
  • Hi, i am also experiencing this issue and I cant find anything relating to this issue. The credential is valid and I still get this annoying error – aj go May 25 '21 at 11:03

2 Answers2

0

I've checked your provide link and not found any problem. In general, There is no problem in writing this way. The problem should be related to your server environment or account as wannadream mentioned.

You can refer to the following links:

"(401) Unauthorized" error when you use an EWS application to impersonate a user in Office365 Dedicated/ITAR

Exchange Web Service API and 401 unauthorized exception

Also, could you please change your account to test it?

Simon Li
  • 303
  • 2
  • 4
0

Shot in the dark, but easy to try: several years ago I was getting unexplained 401s, and found some web post that mentioned adding an empty cookie container to the service object, i.e.:

service.CookieContainer = new CookieContainer();

It seemed to do the trick at the time, but not sure if it's still a thing.

pjneary
  • 1,136
  • 6
  • 8