0
votes

I need to connect to EWS. I am using Microsoft.Exchange.WebServices.NETStandard NuGet package for that. On the Windows machine everything is ok, but on Mac OS I'm getting the error "The remote server returned an error: (401) Unauthorized." Also, if I use just Microsoft.Exchange.WebServices it works fine, but whole my project is based on .Net Core. Maybe, somebody has encountered this problem.

using System;
using System.Linq;
using Microsoft.Exchange.WebServices.Data;

namespace testExchange
{
    class Program
    {
        static void Main(string[] args)
        {
            var exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP2, TimeZoneInfo.Utc);
            
            string userLogin = "userLogin";
            string userPass = "userPassword";
            string exchangeDomain = "icx";
            string userEmail = "[email protected]";
            string webmail = "https://domain/ews/Exchange.asmx";
            exchangeService.Credentials = new WebCredentials(userLogin, userPass, exchangeDomain);
            exchangeService.Url = new Uri(webmail);

            string name = exchangeService.ResolveName(userEmail, ResolveNameSearchLocation.DirectoryOnly, true).Result.First().Contact.DisplayName;

            Console.WriteLine(name);
        }
    }
}
1

1 Answers

0
votes
AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);

It helped. Just add this. Thanks so much to Ahh Bui

Post where I found the answer