How to Send an Email Using Windows PowerShell
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. And today, we’ll talk about
Send-MailMessage
, a cmdlet to send emails from PowerShell, as well as other ways to handle this.
Table of Contents hide
- The simplest script to send an email with PowerShell
- Can I send an HTML email or an email with attachments from PowerShell to multiple recipients?
- Send an email from PowerShell using the Gmail SMTP server
- SMTP settings for popular email providers
- Send emails from PowerShell using EASendMail
- Send emails from PowerShell using System.Net.Mail API
- To wrap up
The simplest 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.
All you need is to insert 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.
Mailtrap will help us to check whether it works. Sign up and go to your Demo inbox. Find SMTP credentials and tweak the script like this:
Here, we’ve just specified the SMTP host. The addresses of the recipient and the sender are not real, as you might have guessed. And that’s one of the benefits of using a fake SMTP server. You don’t have to deal with actual email to test the email workflow. Alternatively, you can use a dummy email, but it can be a poor testing practice. Learn why in our blog post Why Using Dummy Email for Testing Just Doesn’t Work.
Once you copy and paste this script to PowerShell and hit enter, a window requesting SMTP credentials (username and password) will pop up. Fill them in with those of Mailtrap, and that’s it. Check your Demo inbox and find your message.
Can I send an HTML email or an email with attachments from PowerShell to multiple recipients?
Yes you can!
Send-MailMessage
lets you pump up your email with the 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:
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:
As you noticed, we used
$Body +=
to add a new line to the body text of the message. And that’s how the email looks in the Mailtrap inbox.Send an email from PowerShell using the Gmail SMTP server
Everything is cool when you use a fake SMTP server for testing. But what if we send a message via a real one like Gmail. In this case, add
smtp.gmail.com
as the SMTP host and use real email addresses for a sender (your Gmail address) and a recipient.
We made an attempt and filled in credentials in the pop-up window. But the server replied with an 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. We did it, and PowerShell successfully delivered our message to the destination. Discover more about SMTP response codes and commands in our dedicated blog post. SMTP settings for popular email providers
Not everyone is a Gmail user. So, the following data might be to the point if you use another SMTP host.
Send emails from PowerShell using EASendMail
EASendMail is an SMTP component that supports such protocols as SMTP, ESMTP (extended SMTP), EWS (Exchange Web Service), and WebDAV (Web Distributed Authoring and Versioning) protocols. The component is mostly used to send emails in COM/.NET/.NET Core environment applications and apps built with:
- ASP
- VB
- VBA
- VC++
- C++/CLI
- C#
- VB.NET
- JavaScript
- ASP.NET
- Delphi
Also, you can use EASendMail to send emails from PowerShell. God only knows why you may need it but here is a guide for this:
- Step 1: Install EASendMail using the installer
- Step 2: Create the following script
- Step 3: Run the script and check the Mailtrap Demo inbox. Here is the message.
Mass mailing from PowerShell using EASendMail Service Queue
EASendMail SMTP component is useful for sending mass emails. For this, you don’t have to code multiple threadings. All you need is EASendMail Service that can send messages with multiple threadings automatically in background queue. Take a look at the following script for mass mailing of a message with an HTML template:
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. It is a namespace that contains classes to send electronic messages to the SMTP server. The delivery is carried out using the
SmtpClient.Send
or .Send
method. Let’s take a look at the following example:
Check out Mailtrap’s Demo inbox and voila – it’s in. Alternatively, you can use the
.Send
method and modified syntax like this one:To wrap up
The System.Net.Mail namespace is also used to create and send messages in ASP.NET C# that we’ve also blogged about. And that’s it for today. We hope that this blog post broadened your skills in using PowerShell. Follow our blog and discover useful expertise for your projects.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.