Search code examples
exchangewebservices

Cannot connect to Exchange 2019 programmatically


My code connects to Exchange 2017 just fine, but after upgrading to 2019 it has stopped working.

The exception is:

<Trace Tag="AutodiscoverConfiguration" Tid="1" Time="2020-04-25 00:48:18Z">
Trying to call Autodiscover for [email protected] on https://subdomain.mydomain.com/autodiscover/autodiscover.xml.
</Trace>
<Trace Tag="AutodiscoverConfiguration" Tid="1" Time="2020-04-25 00:48:19Z">
 failed: WebException (The underlying connection was closed: An unexpected error occurred on a send.)
</Trace>

As you can see, I have enabled tracing. The certificates are just fine, and I can connect to https://subdomain.mydomain.com/autodiscover/autodiscover.xml from a browser.

What can be wrong?

Here is the code:

Dim service As New ExchangeService(ExchangeVersion.Exchange2013_SP1)

service.TraceListener = New TraceListener()
service.TraceEnabled = True
service.Credentials = New WebCredentials(Username, Password)
service.AutodiscoverUrl(EmailAddress, AddressOf RedirectionCallback)

Solution

  • The most likely cause is TLS 1.2 as Exchange 2019 will only accept TLS 1.2 connection, I would suggest you compile the most recent version of the EWS Managed API from github https://github.com/OfficeDev/ews-managed-api, check the version on the .net framework you using supports it https://learn.microsoft.com/en-us/dotnet/framework/network-programming/tls and you can also explicitly set the TLS version

    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;