0
votes

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("[email protected]", "password");
  service.TraceEnabled = true;
  service.TraceFlags = TraceFlags.All;
  service.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback);
  EmailMessage email = new EmailMessage(service);
  email.ToRecipients.Add("[email protected]");
  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://docs.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.

2
401 is pretty straight, either your credential is wrong or EWS was not configured properly on your Exchange Server.wannadream
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 erroraj go

2 Answers

0
votes

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?

0
votes

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.