Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Authentication against EWS stopped working

The following code worked at last at the 12. Oct. 2022. Today I started it and it throws an error 401.

Has something changed on Microsoft side or what could have happened and how do I get it working again?

static void Main(string[] args)
    {
        ExchangeService _service;
        _service = new ExchangeService
        {
            Credentials = new WebCredentials(myusername,mypassword)
            //Credentials = new NetworkCredential(myusername,mypassword)
        };
        string serviceURI = "https://outlook.office365.com/EWS/Exchange.asmx";
        _service.Url = new Uri(serviceURI);
        EmailMessage message = new EmailMessage(_service);
        message.Subject = "Test email";
        message.Body = "<h1>Test-E-Mail</h1>";
        message.Body.BodyType = BodyType.HTML;
        //message.Attachments.AddFileAttachment(reportname);
        message.ToRecipients.Add(recipient);
        message.SendAndSaveCopy();
    }

Thrown error at row "SendAndSaveCopy":

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Microsoft.Exchange.WebServices.Data.ServiceRequestException: ‘The request failed. The remote server returned an error: (401) .’

>Solution :

As announced in September 2021, Microsoft began permanently disabling basic authentication (as you are using here) for EWS in Exchange Online starting 1st October 2022. You will therefore need to migrate to using OAuth 2.0 authentication. There is some documentation on using OAuth 2.0 to authenticate to EWS here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading