When Written: Feb 2022
Microsoft, like most platform providers quite rightly drop older and less secure technologies. This means that applications you wrote a few years ago may eventually fail unless you do something about it. This is one of the frustrating things about software development. Going back to a client and trying to convince them to pay for a partial re-write of their code for no apparent gain to them is often tricky. I am currently in re-writing some five thousand lines of code to bring it up to date and move more current technology, the client in this case is my own company. I shan’t bore you with all the changes but one that caused me a couple of days of head scratching and the usual google/ Bing searches did not help.
This one application emailed various reports, Microsoft now recommend using System.Net.Mail the code for which is fairly self expiatory ( see below) . I wanted to use our Online Office365 exchange server to send the emails to minimise issues with being blocked by spam engines. Anyone with an Office365 account ( you all have one by now don’t you? ) can send emails via smtp.office365.com using one of your usernames and password. Obviously you want to securely connect via SSL and here is where things can start to go wrong. Microsoft are depreciating TLS1.0 and 1.1 from their products, whilst you can use various settings to keep these enabled for your domain, there is a good reason or two not to use them after all why write a new program and have to but bodge it to still use insecure technologies?
I figured that the reason my code was failing to send emails to the server was because the SMTPSSL protocol used was incorrect, the fix is fairly simple and can be forced with the line:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault
But this was not available to me, so looked at the properties of the application and it was built for .NET framework 4, normally fine. However this just gave me:
SecurityProtocolType.SSL3
SecurityProtocolType.Tls
Change to framework to 4.7 and this changes to:
SecurityProtocolType.SSL3
SecurityProtocolType.Tls
SecurityProtocolType.Tls11
SecurityProtocolType.Tls12
SecurityProtocolType.SystemDefault
This last option allows the operating system to determine the most secure transprort to use to comunicate with the email server. Should you be building with .NetCore you should also be OK. But it was quite frustrating to find this fix in only the later versions of .NetFramework.
I hope this helps.
SAMPLE Code
Imports System.Net EmailTo = "user@gmail.com" Dim e_mail As New MailMessage() Using Smtp_Server As New SmtpClient Smtp_Server.UseDefaultCredentials = False Smtp_Server.Credentials = New Net.NetworkCredential("xxx@xxxx.com", "psword") System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault Smtp_Server.Port = 25 Smtp_Server.EnableSsl = True Smtp_Server.Host = "smtp.office365.com" emailbody = " Some text for the body of the email" e_mail = New MailMessage() e_mail.From = New MailAddress("xxxx@xxxx.com") e_mail.To.Add(EmailTo) e_mail.Subject = subject e_mail.IsBodyHtml = False e_mail.Body = emailbody Try Smtp_Server.Send(e_mail) Catch ex As Exception Labstatus.Text = "Exception caught in RetryIfBusy(): {0} " & ex.ToString() Application.DoEvents() System.Threading.Thread.Sleep(6000) Smtp_Server.Send(e_mail) End Try e_mail.Dispose() End Using
Article by: Mark Newton
Published in: Mark Newton