Easily send your DevOps reporting by email with this PowerShell function, because the Send-MailMessage cmdlet is obsolete. Of course you’ll be using StartTLS and authenticated SMTP as additional security.
If you’re trying to send an email using the Send-MailMessage cmdlet, it throws a warning on your shell:
WARNING: The command ‘Send-MailMessage’ is obsolete. This cmdlet does not guarantee secure connections to SMTP servers. While there is no immediate replacement available in PowerShell, we recommend you do not use Send-MailMessage at this time. See https://aka.ms/SendMailMessage for more information.
Time for something else, right?
In this post I provide you with a small PowerShell function you can use as to send email over an secured SMTP connection with SMTP authentication and StartTLS. As a framework you can use it in your own scripting, extend it, and you can even turn it into a PowerShell module. The mail function is quite rudimentary and has a lot of assumptions in it. All provided AS-IS.
PowerShell function to send SMTP email
You can use the following function to send a basic email to an email address of your choosing. On your shell, or command line interface / cli, it prompts you for your SMTP authentication password. This way, using a SecureString, your password is not saved in PowerShells history file $env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt
.
Function Send-MyMailMessage($from, $to) {
$mailFrom = "${from}"
$mailTo = "${to}"
$Subject = "Test email"
$Body = "This is my email message"
$SMTPServer = "smtp.example.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587)
$SMTPClient.EnableSsl = $true
$ww = read-host -AsSecureString "Provide your email password."
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("${from}", "$(${ww} | convertfrom-securestring -AsPlainText)");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
}
Code language: PowerShell (powershell)
Be sure to change the SMTP server address on line 6, and maybe the SMTP port 587 on line 7.
When you call Send-MyMailMessage
with arguments $from
and $to
, like:
Send-MyMailMessage [email protected] a[email protected]
Provide your email password.: *****************
Code language: PowerShell (powershell)
and you fill out your SMTP authentication password, an email is sent to admi[email protected] with [email protected] as the sender’s from address.
Instead of using .NET’s SMTP Class Net.Mail.SmtpClient
, you can also use and reference MailKit. I created a small .NET MailKit example on ITFAQ.nl (in Dutch).
Such a small, and rather static, PowerShell mail sending function is ideal for DevOps reporting. Of course you can expand the function with additional input variables and command line arguments, etc.
Show Your Support

If you want to step in to help me cover the costs for running this website, that would be awesome. Just use this link to donate a cup of coffee ☕($10 USD or €10 EUR for example). And please share the love and help others make use of this website. Thank you very much! <3 ❤️