How to setup ExchangeService with TLS 1.2 in C#?

Milano, Gianluca 1 Reputation point
2022-03-30T12:51:11.333+00:00

Hi,

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"];
}
try
{ 
   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:
https://techcommunity.microsoft.com/t5/exchange-team-blog/exchange-server-tls-guidance-part-1-getting-ready-for-tls-1-2/ba-p/607649
https://techcommunity.microsoft.com/t5/exchange-team-blog/exchange-server-tls-guidance-part-2-enabling-tls-1-2-and/ba-p/607761
https://techcommunity.microsoft.com/t5/exchange-team-blog/exchange-server-tls-guidance-part-3-turning-off-tls-1-0-1-1/ba-p/607898

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,
Gianluca

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.
555 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Glen Scales 4,441 Reputation points
    2022-03-31T22:56:27.76+00:00

    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

    https://learn.microsoft.com/en-us/mem/configmgr/core/plan-design/security/enable-tls-1-2-client

    I would check the version of the framework you addin is complied to use eg if its VSTO eg https://learn.microsoft.com/en-us/visualstudio/vsto/migrating-office-solutions-to-the-dotnet-framework-4-or-later?view=vs-2022 . 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.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.