Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Send an Email Using Windows PowerShell

December 16, 2022
in Blog, Powershell
0
ADVERTISEMENT

Table of Contents

Windows PowerShell is mostly known as a command-line shell used to solve some administration tasks in Windows and apps running on this OS. At the same time, it is a scripting language that allows you to tailor cmdlets – lightweight commands to perform specific functions. 

In this post, we’ll talk about Send-MailMessage, a cmdlet to send emails from PowerShell, as well as other ways to handle this.

Script to send an email with PowerShell

Let’s start with simple things. Here is a one-line script based on the Send-MailMessage cmdlet you can use right now to send an email from PowerShell using SMTP protocol.

Send-MailMessage `
    -To "[email protected]" `
    -From "[email protected]" `
    -Subject "This is a test message from PowerShell" `
    -Body "Hello World!" `
    -Credential (Get-Credential) `
    -SmtpServer "smtp.office365.com" `
    -Port 587 `
    -UseSsl    #If mail server not support SSL/TLS, let remove this parameter

All you need is to replace the email address of a sender and a recipient, as well as specify the SMTP server you’re going to use. Then copy-paste this script to your PowerShell and press Enter.

Note Note: Some mail servers allow using port 25. So, you can use -Port 25 instead of -Port 587
How to Send an Email Using Windows PowerShell

As you can see, the email was sent from Tom to admin using PowerShell commands.

How to Send an Email Using Windows PowerShell
ADVERTISEMENT

Send an HTML email or an email with attachments from PowerShell

Send-MailMessage lets you pump up your email with many parameters, including HTML content, priority, and so on. Also, you can send emails to multiple recipients by specifying them in the corresponding parameter. Here is what you can make use of:

ParameterDescription
-ToEmail address of a recipient or recipients
-BccEmail address of a BCC recipient or recipients
-CcEmail address of a CC recipient or recipients
-FromSender’s email address
-SubjectEmail subject
-BodyEmail body text
-BodyAsHtmlDefines that email body text contains HTML
-AttachmentsFilenames to be attached and the path to them
-CredentialAuthentication to send the email from the account
-SmtpServerName of the SMTP server
-PortPort of the SMTP server
-EncodingThe encoding for the body and subject
-PriorityDefines the level of priority of the message. Valid options are: Normal (default parameter) Low High
-UseSslConnection to the SMTP server will be established using the Secure Sockets Layer (SSL) protocol

So, let’s make a script that will send an email containing HTML text and an attachment. This time, we’ll define the parameters in advance and then refer to them in the Send-MailMessage cmdlet:

$From = "[email protected]"
$To = "[email protected]", "[email protected]"
$Cc = "[email protected]"
$Attachment = "C:\Temp\pj2022.zip"
$Subject = "New updated avata"
$Body = "<h2>Guys, This is new project!</h2><br><br>"
$Body += "Give me the feedbacks!"
$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"

Send-MailMessage -From $From -to $To -Cc $Cc `
    -Subject $Subject -Body $Body -BodyAsHtml `
    -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl `
    -Credential (Get-Credential) `
    -Attachments $Attachment
Note Tip: As you noticed, we used to add a new line to the body text of the message.

As you can see, the email was sent with the formatted texts and an attachment.

How to Send an Email Using Windows PowerShell

Send an email from PowerShell using the other SMTP servers

In case you want to send emails via Gmail’s SMTP server, simply add smtp.gmail.com as the SMTP host.

In some cases, you would get the error – 551 Authentication Required. To fix this issue, you need to turn on the Less secure app access in the Security tab of your Google Account.

Send-MailMessage `
    -To "[email protected]" `
    -From "[email protected]" `
    -Subject "This is a test message from PowerShell" `
    -Body "Hello World!" `
    -Credential (Get-Credential) `
    -SmtpServer "smtp.gmail.com" `
    -Port 587 ` 
    -UseSsl

Using the below example to send email from Office 365 using PowerShell.

Send-MailMessage `
    -To "[email protected]" `
    -From "[email protected]" `
    -Subject "This is a test message from PowerShell" `
    -Body "Hello World!" `
    -Credential (Get-Credential) `
    -SmtpServer "smtp.office365.com" `
    -Port 587 `
    -UseSsl

The SMTP settings for popular email providers:

ServiceSMTP serverPortConnection
Gmailsmtp.gmail.com587, 25, 465TLS, TLS, SSL
Office 365smtp.office365.com25, 587TLS
Outlook.comsmtp-mail.outlook.com25, 587TLS
Yahoo mailsmtp.mail.yahoo.com25, 465, 587SSL, TLS
Windows Live Hotmailsmtp.live.com25, 587SSL, TLS
Zohosmtp.zoho.com465, 587SSL, TLS

Send emails from PowerShell using System.Net.Mail API

Send-MailMessage cmdlet is the most common option for sending emails from PowerShell. But this was not always the case. It became available starting from PowerShell 3.0 and was based on System.Net.Mail API.

$EmailFrom = "[email protected]"
$EmailTo = "[email protected]"
$Subject = "System.Net.Mail API"
$Body = "Send emails from PowerShell!"
$SMTPServer = "smtp.office365.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("[email protected]", "PassW0rd");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
How to Send an Email Using Windows PowerShell
ADVERTISEMENT
5/5 - (1 vote)
Previous Post

How to Install Locate Command to Find Files in Linux Systems

Next Post

Enable SMTP Authentication for a Single Mailbox in Microsoft 365

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

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

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory