Windows Server - How to Send SMTP Auth E-Mail with PowerShell Using the System.Net.Mail.MailMessage and Net.Mail.SmtpClient .NET Framework objects with PowerShell to send authenticated e-mail messages through a secure SMTP server

Event Viewer: come inviare notifiche e-mail dal Registro Eventi con uno script Powershell

A couple of years ago I published an article explaining how to send notification E-Mail messages with PowerShell. I wrote that post to demonstrate how a simple notification script could allow the Windows Event Viewer to send its warnings through e-mail messages even when the  native "send to e-mail" feature was deprecated (such as in Windows Server 2012 and later).

Truth to be told, that little PowerShell script I wrote could also be used on a wider scenario - basically, every time you need to create custom messages and automate them using an internal relay service, such as:

  • The default SMTP server shipped with Windows Server.
  • A drop-in Windows porting to the Linux Sendmail, such as Fake Sendmail (or other similar alternatives).
  • A full-fledged SMTP service solution such as hMailServ, a neat open-source MTA / MHS service which I extensively reviewed a while ago.

All of the alternatives above can be properly configured to securely allow the relay of e-mail messages from the local machine to the WAN, either using unauthenticated (yet IP-restricted) access or relying to the internal Window authentication: this is an ideal scenario for the Send-MailMessage cmdlet that I used within the script to actually send the e-mail messages.

What if you don't have (or don't want to rely to) an internal relay service, thus needing to use an external SMTP service? In such scenario, you'll most likely need to send the SMTP authentication data to the external service, and maybe even add or configure some custom message headers as well. Unfortunately, the Send-MailMessage cmdlet comes with a fairly limited amount of features and doesn't allow that kind of stuff.

Luckily enough there are better alternatives that we can use to achieve such result: I'm talking about the System.Net.Mail.MailMessage and Net.Mail.SmtpClient. two powerful .NET Framework objects that we can invoke with PowerShell. I've successfully used these classes when I developed RunningLow, a free PowerShell script that checks for low disk space and send e-mail to System Administrators (also available on GitHub).

Here's how RunningLow sends its e-mail notifications through an external SMTP server:

I stripped the RunningLow source code down to its SMTP-relevant parts. As we can see, the code is splitted into three parts:

  • the SMTP configuration, where we specify the various SMTP server connection and authentication parameters.
  • the E-Mail configuration, where we create the System.Net.Mail.MailMessage object and setup the e-mail message stuff: from/to addresses, subject, body, and so on.
  • the Sending phase, where we create the System.Net.Mail.SmtpClient object and actually send the e-mail message.

There are a lot of things that we can do with these classes. For example, here's a list of the various properties that we can use to configure our System.Net.Mail.MailMessage object:

Windows Server - How to Send SMTP Auth E-Mail with PowerShell

As we can see, there are a lot of things that can be done by mixing PowerShell cmdlets and these features, such as: adding attachments, importing the message body content from an external file using Get-Content, and so on.

That's it, at least for now: for additional info regarding the System.Net.Mail namespace, I strongly suggest to read the MSDN official docs.

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

One Comment on “Windows Server - How to Send SMTP Auth E-Mail with PowerShell Using the System.Net.Mail.MailMessage and Net.Mail.SmtpClient .NET Framework objects with PowerShell to send authenticated e-mail messages through a secure SMTP server

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.