How to setup ExchangeService with TLS 1.2 in C#?

Milano, Gianluca 1 Reputation point


I have an Outlook (2013) AddIn written in C#, which worked fine when using it with TLS 1.0/1.1 enabled. However, when disabling TLS 1.2 on Exchange Server, the code does not work anymore. The code did not change when TLS 1.0/1.1 was disabled.
The error message is: "The request failed .The underlying connection was closed: An unexpected error occurred on a receive."
Here is a code snippet:

this.service = new ExchangeService();
this.service.UseDefaultCredentials = true;

if (this.registryValues.ContainsKey("ExchangeUri"))
   this.service.Url = new Uri(this.registryValues["ExchangeUri"]); 
string distributionGroup = string.Empty;
if (this.registryValues.ContainsKey("VerifierDistributionList"))
    distributionGroup = this.registryValues["VerifierDistributionList"];
   ExpandGroupResults myGroupMembers = this.service.ExpandGroup(distributionGroup);

// checking, if my users EmailAddress is in the group
catch (System.Exception e)
//Showing the exception in a message box

I am using .NET 4.8 to build the project, which is also installed on the Exchange Server. The Exchange Server itself is Exchange Server 2013 with the latest update (CU23).
To setup TLS 1.2 we followed this post:

This looks like an issue on how I created the ExchangeService object, but I'm still wondering why it worked before. I tried adding System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault; (which should be the default anyway for .NET 4.8) or System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; but for both options the error occurs. Interestingly, I can force the same error, when I use SystemDefault, when TLS 1.0/1.1 are enabled.
Do I need to change some definition in my code or is it an issue with Outlook 2013?

Best Regards,

Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
469 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Glen Scales 4,421 Reputation points

    I can force the same error, when I use SystemDefault, when TLS 1.0/1.1 are enabled.

    It sounds like this is to do with the version of the TLS the client is trying to use (this would be you Outlook clients where the Addin is deployed) you should be able to force it to use the latest using the following reg entries

    I would check the version of the framework you addin is complied to use eg if its VSTO eg . If you haven't touched that code in a while it most likely going to be use .net 3.5x which would explain the behaviour your getting

    1 person found this answer helpful.