Send email with PowerShell

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 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 = ""
  $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:

Provide your email password.: *****************Code language: PowerShell (powershell)

and you fill out your SMTP authentication password, an email is sent to with 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 (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.

Protip: Donate $10, 20 or 30 through Paypal (or see my donate page) and support this site. Thank you <3

Jan Reilink

Hi, my name is Jan. I am not a hacker, coder, developer or guru. I am merely an application manager / systems administrator, doing my daily thing at Embrace - The Human Cloud. In the past I worked for clidn and Vevida. With over 20 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, websites & optimization. I blog at