Search code examples
asp.netsslwebformssmtpweb-config

How to add SecurityProtocolType.Tls12 support to Mailsetting in web.config in asp.net webform 4.5 application


I have old asp.net webform application (4.5) and due to security TLS has been upgraded to TLS 1.2 & due to which Mail feature which depend on TLS 1.0 are not working.

Below code works for if server supports TLS 1.0 but on new server TLS 1.0 & TLS 1.1 is disabled so below code in web.config is not working any more, it doesn't give any error but email are not working for default functionality such as password reset for asp.net membership module which depend on below code defined in web.config

 <system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network host="outlook.office365.com" port="587" enableSsl="true" userName="[email protected]" password="Passward" />
      </smtp>
    </mailSettings>
  </system.net>

if same was to be done in code .cs file then we can add following code and it will add TLS 1.2 support and code works but i am not sure how to add same to the mailsetting in web.config for SMTP in above mentioned code. any pointer as i am not able to find syntax for same on net

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

Solution

  • Did you eventually solve this problem? I have a related issue, though I will suggest that the problem be better described to reflect the main problem.

    It would be better that you share your error and also mention all that you have attempted towards solving the problem.

    Your answer did not atrract much contribution because it suggests with the tone of the question that providing a code for enforcing TLS will solve the problem which will not be always true.

    Here is the answer to your question:

    In .NET, you can enforce a specific version of TLS by setting the ServicePointManager.SecurityProtocol property. This property specifies the security protocol used to authenticate and encrypt connections.

    Here's how you can enforce TLS 1.2, for example:

    c#

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

    You should set this property before sending your email. In an ASP.NET application, you might set this in the Application_Start method in Global.asax.cs or in the constructor of your mail sending class.

    For your ASP.NET configuration, you could specify TLS 1.2 in the <system.net> section using the targetFramework attribute. Here's how you can do it:

    <system.net>
     <mailSettings>
       <smtp deliveryMethod="Network" from="*********">
        <network host="**********" port="***" 
        userName="*******" 
        password="*********" 
        useDefaultCredentials="true" enableSsl="true" 
        targetFramework="4.5"/>
       </smtp>
     </mailSettings>
    </system.net>
    

    In the above configuration, targetFramework is set to 4.5, indicating that TLS 1.2 will be used.

    Please note that enforcing a specific version of TLS might affect compatibility with certain servers or clients, so ensure that TLS 1.2 is supported by all parties involved in the email communication. Additionally, you should stay up-to-date with security best practices and consider updating to newer versions of TLS as they become available.