1

Sending certified mail (System.Web.Mail.MailMessage) on port 465 (tls1.2) only fails on windows server 2008 r2

I have a function based on the framework 4.5 but that uses an old library (System.Web.Mail.MailMessage) to send an email through a smtp server on port 465 (certified mail tls1.2), the function runs correctly on windows 10 and windows server 2012 but it fails on windows server 2008 r2. the error is of type System.Web.HttpException indicating that it can not reach the server but doing the telnet on port 465 works. What's wrong with windows server 2008 r2?

try
{
System.Web.Mail.MailMessage newMail = new System.Web.Mail.MailMessage();
newMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "server");
newMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
newMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
newMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
newMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "username");
newMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "pwd");
newMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");

newMail.From = "mail";
newMail.To = "mail";
newMail.Subject = "test web Mail";
newMail.BodyFormat = System.Web.Mail.MailFormat.Html;
newMail.Body = "body....";
newMail.Priority = System.Web.Mail.MailPriority.High;

System.Web.Mail.SmtpMail.SmtpServer = "smtpserver:465";
System.Web.Mail.SmtpMail.Send(newMail);

Console.WriteLine("Email Send");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

System.We.HttpException (0x80004005) the transport failed to connect to the server

Angelo
  • 31
  • 6
  • Is `smtpserver:465` the actual value you use in the code or is it something you replace with an actual smtp server address? – Imantas Jan 04 '19 at 14:03
  • smtpserver is an example the code I reported works correctly on windows 10 and windows server 20012 inserting the correct parameters – Angelo Jan 04 '19 at 14:05
  • Have you done https://stackoverflow.com/a/45383835/11683? – GSerg Jan 04 '19 at 14:05
  • Also, double check if [TLS 1.2 is enabled](https://support.microsoft.com/en-us/help/4019276/update-to-add-support-for-tls-1-1-and-tls-1-2-in-windows) on the server itself. – Imantas Jan 04 '19 at 14:07
  • GSerg I tried to put the following line at the beginning. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; but it still does not work. – Angelo Jan 04 '19 at 14:33
  • Telnet may work, but I bet it is not using TLS 1.2 ;) – TomTom Jan 04 '19 at 15:34
  • telnet was mentioned only to indicate that the port was open – Angelo Jan 04 '19 at 15:40

1 Answers1

-2

I solved by enabling the tls1.2 following this guide or this suggest by Imantas

In windows server 2008 R2 the tls1.2 protocol must be enabled by inserting in the system registry if not existing the following Key:

Under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols Add Folder TLS 1.2 and in folder TLS 1.2 add subfolder Client and subfolder Server in both folders enter the DisabledByDefault key set to 0 and the Enabled key set to 1

Goodbye StackExchange
  • 21,680
  • 7
  • 47
  • 83
Angelo
  • 31
  • 6