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)
}
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:
PS > Send-MyMailMessage jan@example.com admin@example.net
Provide your email password.: *****************
and you fill out your SMTP authentication password, an email is sent to admin@example.net with jan@example.com as the sender’s from address.
Instead of using .NET’s SMTP Class Net.Mail.SmtpClient, you can also use and reference MailKit.
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.
Frequently Asked Questions
The Send-MailMessage cmdlet in PowerShell is considered obsolete and should be avoided for sending emails. It lacks support for modern email authentication and secure connections, making it potentially insecure. Microsoft recommends using alternative solutions like the Send-MgUserMail cmdlet or the Mailkit module.
Conclusion
This post showed you how to send email with PowerShell using Net.Mail.SmtpClient, because the PS Send-MailMessage cmdlet is obsolete. The provided function uses StartTLS for encryption and SMTP authentication to your SMTP server for additional security.
Summary
- The Send-MailMessage cmdlet in PowerShell is obsolete and lacks support for secure email standards.
- You can send email with PowerShell using a custom function that employs StartTLS and SMTP authentication.
- This function prompts for your SMTP password securely and prevents it from being saved in history.
- You can easily modify the SMTP server and port to suit your needs, and expand the function for more capabilities.
- The article provides an alternative approach to sending emails safely in PowerShell for DevOps reporting.



[…] or .NET, can come in handy when sending DevOps reporting mails. This is a nice addition to my Send email wiht PowerShell post, but without additional […]
[…] how to send authenticated SMTP email over TLS from WordPress! Send email with PowerShell and how to send authenticated SMTP over a TLS encrypted connection, in PHP, […]