Skip to content

How to Send email with PowerShell

We are familiar with the Send-MailMessage cmdlet when sending emails via PowerShell. Unfortunately, the Send-MailMessage cmdlet is obsolete and not recommended for use anymore. So, what is the Send-MailMessage alternative? The answer is Send-MailKitMessage. In this article, you will learn how to send emails with PowerShell.

Send-MailMessage cmdlet is obsolete

The Send-MailMessage cmdlet is popular to send an email message via PowerShell. Unfortunately, the Send-MailMessage cmdlet is obsolete, and you should not use it anymore.

Note: The Send-MailMessage cmdlet is obsolete. This cmdlet doesn’t guarantee secure connections to SMTP servers. While there is no immediate replacement available in PowerShell, we recommend you do not use Send-MailMessage.

Now that no replacement is available in the default PowerShell cmdlets, is there a module that we can install and use to send email messages from PowerShell? Yes, there is. The Send-MailKitMessage module is an excellent replacement for the Send-MailMessage cmdlet.

Configure SMTP relay

Read the below articles if you want to set up an SMTP relay:

Install Send-MailKitMessage module

To install the Send-MailKitMessage module on your system, go through the below steps.

1. Set Windows PowerShell Execution Policy

By default, we can’t install scripts. To require all PowerShell scripts that you download from the internet are signed by a trusted publisher, run PowerShell as administrator, and run the cmdlet.

Set-ExecutionPolicy RemoteSigned

Important: Close and re-open the elevated Windows PowerShell window to have the changes apply.

2. Install PowerShellGet module

Run PowerShell as administrator. Run the command Install-Module PowershellGet -Force. When asked to install NuGet provider, press Y and follow with Enter.

Install-Module PowershellGet -Force

If you get an error that it’s unable to install, read the article Unable to install NuGet provider for PowerShell.

3. Install Send-MailKitMessage module

Install the Send-MailKitMessage module.

Install-Module -Name Send-MailKitMessage -Force

Now that the module is installed, let’s look at some examples of how to send emails with PowerShell.

Send-MailKitMessage parameters

You can use the below parameters when using the Send-MailKitMessage module.

# Use secure connection if available ([bool], optional)
$UseSecureConnectionIfAvailable = $true

# Authentication ([System.Management.Automation.PSCredential], optional)
$Credential = [System.Management.Automation.PSCredential]::new("Username", (Read-Host -Prompt "Enter password" -AsSecureString))

# SMTP server ([string], required)
$SMTPServer = "SMTPServer"

# Port ([int], required)
$Port = "PortNumber"

# Sender ([MimeKit.MailboxAddress] http://www.mimekit.net/docs/html/T_MimeKit_MailboxAddress.htm, required)
$From = [MimeKit.MailboxAddress]"SenderEmailAddress"

# Recipient list ([MimeKit.InternetAddressList] http://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, required)
$RecipientList = [MimeKit.InternetAddressList]::new()
$RecipientList.Add([MimeKit.InternetAddress]"Recipient1EmailAddress")

# CC list ([MimeKit.InternetAddressList] http://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, optional)
$CCList = [MimeKit.InternetAddressList]::new()
$CCList.Add([MimeKit.InternetAddress]"CCRecipient1EmailAddress")

# BCC list ([MimeKit.InternetAddressList] http://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, optional)
$BCCList = [MimeKit.InternetAddressList]::new()
$BCCList.Add([MimeKit.InternetAddress]"BCCRecipient1EmailAddress")

# Subject ([string], optional)
$Subject = [string]"Subject"

# Text body ([string], optional)
$TextBody = [string]"TextBody"

# HTML body ([string], optional)
$HTMLBody = [string]"HTMLBody"

# Attachment list ([System.Collections.Generic.List[string]], optional)
$AttachmentList = [System.Collections.Generic.List[string]]::new()
$AttachmentList.Add("Attachment1FilePath")

# Splat parameters
$Parameters = @{
    "UseSecureConnectionIfAvailable" = $UseSecureConnectionIfAvailable    
    "Credential"                     = $Credential
    "SMTPServer"                     = $SMTPServer
    "Port"                           = $Port
    "From"                           = $From
    "RecipientList"                  = $RecipientList
    "CCList"                         = $CCList
    "BCCList"                        = $BCCList
    "Subject"                        = $Subject
    "TextBody"                       = $TextBody
    "HTMLBody"                       = $HTMLBody
    "AttachmentList"                 = $AttachmentList
}

# Send message
Send-MailKitMessage @Parameters

Send email with PowerShell examples

Here are some templates that you can use to send emails with PowerShell.

The following parameters are required:

  • SMTPServer
  • Port
  • From
  • RecipientList

Send email to recipient

This example sends an email message from a sender to a recipient.

# SMTP server ([string], required)
$SMTPServer = "mail.exoip.com"

# Port ([int], required)
$Port = "25"

# Sender ([MimeKit.MailboxAddress] http://www.mimekit.net/docs/html/T_MimeKit_MailboxAddress.htm, required)
$From = [MimeKit.MailboxAddress]"Alysia.Maverick@exoip.com"

# Recipient list ([MimeKit.InternetAddressList] http://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, required)
$RecipientList = [MimeKit.InternetAddressList]::new()
$RecipientList.Add([MimeKit.InternetAddress]"john.doe@gmail.com")

# Subject ([string], optional)
$Subject = [string]"This is the subject"

# Text body ([string], optional)
$TextBody = [string]"This is the text body"

# Splat parameters
$Parameters = @{
    "SMTPServer"    = $SMTPServer
    "Port"          = $Port
    "From"          = $From
    "RecipientList" = $RecipientList
    "Subject"       = $Subject
    "TextBody"      = $TextBody
}

# Send message
Send-MailKitMessage @Parameters

This is how it looks in the recipient’s inbox.

Send email with PowerShell example basic

Send email with attachment

This example sends an email message with an attachment to multiple recipients.

# SMTP server ([string], required)
$SMTPServer = "mail.exoip.com"

# Port ([int], required)
$Port = "25"

# Sender ([MimeKit.MailboxAddress] http://www.mimekit.net/docs/html/T_MimeKit_MailboxAddress.htm, required)
$From = [MimeKit.MailboxAddress]"Alysia.Maverick@exoip.com"

# Recipient list ([MimeKit.InternetAddressList] http://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, required)
$RecipientList = [MimeKit.InternetAddressList]::new()
$RecipientList.Add([MimeKit.InternetAddress]"john.doe@gmail.com")
$RecipientList.Add([MimeKit.InternetAddress]"jane.doe@outlook.com")

# Subject ([string], optional)
$Subject = [string]"Sending the Attachment"

# Text body ([string], optional)
$TextBody = [string]"See the CSV file attachment."

# Attachment list ([System.Collections.Generic.List[string]], optional)
$AttachmentList = [System.Collections.Generic.List[string]]::new()
$AttachmentList.Add("C:\temp\File.csv")

# Splat parameters
$Parameters = @{
    "SMTPServer"     = $SMTPServer
    "Port"           = $Port
    "From"           = $From
    "RecipientList"  = $RecipientList
    "Subject"        = $Subject
    "TextBody"       = $TextBody
    "AttachmentList" = $AttachmentList
}

# Send message
Send-MailKitMessage @Parameters

This is how it looks in the recipient’s inbox.

Send email with PowerShell example with attachment

Send email to mailing list

This example sends an email message to a mailing list with additional recipients in the CC and BCC fields.

# SMTP server ([string], required)
$SMTPServer = "mail.exoip.com"

# Port ([int], required)
$Port = "25"

# Sender ([MimeKit.MailboxAddress] http://www.mimekit.net/docs/html/T_MimeKit_MailboxAddress.htm, required)
$From = [MimeKit.MailboxAddress]"Alysia.Maverick@exoip.com"

# Recipient list ([MimeKit.InternetAddressList] http://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, required)
$RecipientList = [MimeKit.InternetAddressList]::new()
$RecipientList.Add([MimeKit.InternetAddress]"IT-Team@exoip.com")

# CC list ([MimeKit.InternetAddressList] http://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, optional)
$CCList = [MimeKit.InternetAddressList]::new()
$CCList.Add([MimeKit.InternetAddress]"john.doe@gmail.com")

# BCC list ([MimeKit.InternetAddressList] http://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, optional)
$BCCList = [MimeKit.InternetAddressList]::new()
$BCCList.Add([MimeKit.InternetAddress]"david.bowen@exoip.com")

# Subject ([string], optional)
$Subject = [string]"To all the members"

# Text body ([string], optional)
$TextBody = [string]"Check out the magic."

# Splat parameters
$Parameters = @{
    "SMTPServer"     = $SMTPServer
    "Port"           = $Port
    "From"           = $From
    "RecipientList"  = $RecipientList
    "CCList"         = $CCList
    "BCCList"        = $BCCList
    "Subject"        = $Subject
    "TextBody"       = $TextBody
}

# Send message
Send-MailKitMessage @Parameters

This is how it looks in the recipient’s inbox.

Send email with PowerShell including CC and BCC

Send email over a secure connection (TLS)

Transport Layer Security (TLS) is a protocol that encrypts email messages for security and privacy.

This example sends an email message from a sender that needs to authenticate to a recipient over a secure connection (TLS).

# Use secure connection if available ([bool], optional)
$UseSecureConnectionIfAvailable = $true

# Authentication ([System.Management.Automation.PSCredential], optional)
$Credential = [System.Management.Automation.PSCredential]::new("Alysia.Maverick@exoip.com", (Read-Host -Prompt "Enter password" -AsSecureString))

# SMTP server ([string], required)
$SMTPServer = "mail.exoip.com"

# Port ([int], required)
$Port = "587"

# Sender ([MimeKit.MailboxAddress] http://www.mimekit.net/docs/html/T_MimeKit_MailboxAddress.htm, required)
$From = [MimeKit.MailboxAddress]"Alysia.Maverick@exoip.com"

# Recipient list ([MimeKit.InternetAddressList] http://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, required)
$RecipientList = [MimeKit.InternetAddressList]::new()
$RecipientList.Add([MimeKit.InternetAddress]"john.doe@gmail.com")

# Subject ([string], optional)
$Subject = [string]"Sending with Port 587"

# Text body ([string], optional)
$TextBody = [string]"Sending the message on Port 587 (TLS)"

# Splat parameters
$Parameters = @{
    "UseSecureConnectionIfAvailable" = $UseSecureConnectionIfAvailable
    "Credential"                     = $Credential
    "SMTPServer"                     = $SMTPServer
    "Port"                           = $Port
    "From"                           = $From
    "RecipientList"                  = $RecipientList
    "Subject"                        = $Subject
    "TextBody"                       = $TextBody
}

# Send message
Send-MailKitMessage @Parameters

That’s it!

Read more: Fix Winmail.dat attachment in Office 365 »

Conclusion

You learned how to send emails with PowerShell using the Send-MailKitMessage module. It’s an excellent replacement for the Send-MailMessage cmdlet, which is no longer in use anymore. Install the module and adjust your scripts, or use it to test your outgoing SMTP mail flow.

Did you enjoy this article? You may also like How to configure Microsoft 365 to only accept mail from third-party spam filter. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 0 Comments

Leave a Reply

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