ECatsBlog
  • Home
  • Web Dev
  • Reviews
  • Thoughts
  • ASP
  • SQL
  • Browser
  • Mobile
  • Cloud
  • CMS
  • ECommerce
  • App Hacks
  • Fixes
  • Mapping
  • 3D Printing
  • XAML
  • SEO

Categories

  • 3D Printing (1)
  • App Hacks (5)
  • ASP (22)
  • Browser (13)
  • Cloud (10)
  • CMS (7)
  • ECommerce (6)
  • Fixes (5)
  • Mapping (1)
  • Mobile (12)
  • Reviews (70)
  • SEO (9)
  • SQL (12)
  • Thoughts (60)
  • Uncategorized (1)
  • Web Dev (94)
  • XAML (9)

Archives

  • October 2024 (2)
  • December 2022 (1)
  • October 2022 (1)
  • February 2022 (1)
  • June 2019 (1)
  • January 2019 (175)
  • December 2018 (47)

SMTP Email Client

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

SMTP

  • Previous IIS8 SMTP setup3 years ago
  • Next Unable to ping VM machine from host3 years ago

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • .net application moving files
  • Dealing with accents on database driven web site
  • Neat feature in Chrome
  • Unable to ping VM machine from host
  • SMTP Email Client

Recent Comments

    2025 ECatsBlog. Donna Theme powered by WordPress