Join us
@nataliiapolomkina ă» Oct 01,2022 ă» 8 min read ă» 2478 views ă» Originally posted on mailtrap.io
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.Â
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 â<recipientâs email address>â -From â<senderâs email address>â -Subject âYour message subjectâ -Body âSome important plain text!â -Credential (Get-Credential) -SmtpServer â<smtp server>â -Port 587
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:
Send-MailMessage -To âjon-snow@winterfell.comâ -From âmother-of-dragons@houseoftargaryen.netâ -Subject âHey, Jonâ -Body âSome important plain text!â -Credential (Get-Credential) -SmtpServer âsmtp.mailtrap.ioâ -Port 587
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.Â
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.
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 = "mother-of-dragons@houseoftargaryen.net"
$To = "jon-snow@winterfell.com", "jorah-mormont@night.watchâ
$Cc = "tyrion-lannister@westerlands.com"
$Attachment = "C:\Temp\Drogon.jpg"
$Subject = "Photos of Drogon"
$Body = "<h2>Guys, look at these pics of Drogon!</h2><br><br>"
$Body += âHe is so cute!â
$SMTPServer = "smtp.mailtrap.io"
$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
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.
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.
Not everyone is a Gmail user. So, the following data might be to the point if you use another SMTP host.
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:
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:
[reflection.assembly]::LoadFile("C:\Program Files (x86)\EASendMail\Lib\net20\EASendMail.dll")
#change the path to the EASendMail.dll file if you have another build of run-time assembly for .NET Framework, .NET Core, or .NET Compact Framework
function SendMailTo($sender, $name, $address, $subject, $body, $htmlFormat) {
$mail = New-Object EASendMail.SmtpMail("TryIt") # you can replace âTryItâ with your license code for EASendMail SMTP component
$mail.From.Address = $sender
$recipient = New-Object EASendMail.MailAddress($name, $address)
$mail.To.Add($recipient) > $null
$mail.Subject = $subject
if($htmlFormat) {
$mail.HtmlBody = $body
}
else {
$mail.TextBody = $body
}
$server = New-Object EASendMail.SmtpServer("smtp.mailtrap.io")
$server.User = "*********"
$server.Password = "*********"
$server.Port = 587
$server.ConnectType = [EASendMail.SmtpConnectType]::ConnectTryTLS
# specify your settings of the SMTP server, username, password, port, and connection type
$smtp = New-Object EASendMail.SmtpClient
$smtp.SendMail($server, $mail)
}
function SendMailFromPowerShell () {
$sender = "mother-of-dragons@houseoftargaryen.net"
$name = "Jon Snow"
$address = "jon-snow@winterfell.com"
$subject = "Happy Samhain"
$body = "Jonny, congrats on Samhain!"
# specify your settings of senderâs email address and name, recipientâs email address, as well as subject and body text of the message
try {
"Start to send email to {0} ..." -f $address
SendMailTo $sender $name $address $subject $body ""
"Email to {0} has been submitted to server!" -f $address
}
catch [System.Exception] {
"Failed to send email: {0}" -f $_.Exception.Message
}
}
SendMailFromPowerShell
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:
[reflection.assembly]::LoadFile("C:\Program Files (x86)\EASendMail\Lib\net20\EASendMail.dll")
#change the path to the EASendMail.dll file if you have another build of run-time assembly for .NET Framework, .NET Core, or .NET Compact Framework
function SendMailToQueue($sender, $name, $address, $subject, $body, $htmlFormat) {
$mail = New-Object EASendMail.SmtpMail("TryIt")
$mail.From.Address = $sender
$recipient = New-Object EASendMail.MailAddress($name, $address)
$mail.To.Add($recipient) > $null
$mail.Subject = $subject
if($htmlFormat) {
$mail.HtmlBody = $body
}
else {
$mail.TextBody = $body
}
$server = New-Object EASendMail.SmtpServer("smtp.mailtrap.io")
$server.User = "*********"
$server.Password = "*********"
$server.Port = 587
$server.ConnectType = [EASendMail.SmtpConnectType]::ConnectTryTLS
# specify your settings of the SMTP server, username, password, port, and connection type
$smtp = New-Object EASendMail.SmtpClient
$smtp.SendMailToQueue($server, $mail)
}
function OpenConnection () {
$connectionString = "Server=localhost\AdminSystem;Database=test;Integrated security=SSPI;MultipleActiveResultSets=True;"
Write-Host(("Connecting database {0} ..." -f $connectionString))
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = $connectionString
# You can change the connection string to yours by specifying user and password: $connectionString = "Server=localhost\AdminSystem;Database=dbname;User Id=user;Password=yourpassword;MultipleActiveResultSets=True;"
$sqlConnection.Open()
Write-Host 'Connected'
return $sqlConnection
}
function BuildHtmlBody ($sqlConnection) {
Write-Host "Building HTML body based on database ..."
$sqlQuery = "SELECT ID, Name, Email, Age, Department FROM users"
$sqlCommand = New-Object System.Data.SqlClient.SqlCommand($sqlQuery, $sqlConnection);
$reader = $sqlCommand.ExecuteReader()
$html = "<!DOCTYPE html><html><body>"
$html += "<div style=""font-family:'Segoe UI', Calibri, Arial, Helvetica; font-size: 14px; max-width: 768px;"">"
$html += "Dear {name}, <br /><br />This is a congrats email on Samhain. <br />"
$html += "Here is full data in table:<br /><br />"
$html +="<table style='border-spacing: 0px; border-style: solid; border-color: #ccc; border-width: 0 0 1px 1px;'>"
while ($reader.Read()) {
$name = $reader.GetString(1)
$address = $reader.GetString(2)
$age = $reader.GetInt32(3)
$department = $reader.GetString(4)
$html += "<tr>"
$html += "<td style='padding: 10px; border-style: solid; border-color: #ccc; border-width: 1px 1px 0 0;'>{0}</td>" -f $name
$html += "<td style='padding: 10px; border-style: solid; border-color: #ccc; border-width: 1px 1px 0 0;'>{0}</td>" -f $address
$html += "</tr>"
}
$reader.Close > $null
$reader.Dispose > $null
$sqlCommand.Close > $null
$sqlCommand.Dispose > $null
return $html
}
function SendMailFromDBToQueue () {
$sender = "mother-of-dragons@houseoftargaryen.net"
$subject = "Happy Samhain"
$sqlConnection = OpenConnection
$bodyTemplate = BuildHtmlBody($sqlConnection)
$sqlQuery = "SELECT ID, Name, Email, Age, Department FROM users"
$sqlCommand = New-Object System.Data.SqlClient.SqlCommand($sqlQuery, $sqlConnection);
$reader = $sqlCommand.ExecuteReader()
while ($reader.Read()) {
$name = $reader.GetString(1).Trim()
$address = $reader.GetString(2).Trim()
$body = $bodyTemplate.Replace("{name}", $name)
Write-Host(("Start to send email to {0} ..." -f $address))
SendMailToQueue $sender $name $address $subject $body "html"
Write-Host(("Email to {0} has been submitted to easendmail service!" -f $address))
}
$reader.Close > $null
$reader.Dispose > $null
$sqlCommand.Close > $null
$sqlCommand.Dispose > $null
$sqlConnection.Close > $null
$sqlConnection.Dispose > $null
}
catch [System.Exception] {
"Failed to send email: {0}" -f $_.Exception.Message
}
}
SendMailFromDBToQueue
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:
$EmailFrom = âmother-of-dragons@houseoftargaryen.netâ
$EmailTo = âjon-snow@winterfell.comâ
$Subject = âHappy Samhainâ
$Body = âJonny, congrats on Samhain!â
$SMTPServer = âsmtp.mailtrap.ioâ
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("<username>", "<password>");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Check out Mailtrapâs Demo inbox and voila â itâs in. Alternatively, you can use the .Send
method and modified syntax like this one:
$smtpFrom = âmother-of-dragons@houseoftargaryen.netâ
$smtpTo = âjon-snow@winterfell.comâ
$messageSubject = âHappy Samhainâ
$messageBody = âJonny, congrats on Samhain!â
$smtpServer = âsmtp.mailtrap.ioâ
$smtp = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$smtp.EnableSsl = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential("94b784b5970edf", "01a5d515011f6e");
$smtp.Send($smtpFrom , $smtpTo, $messageSubject, $messageBody)
Learn how exactly Mailtrap can help you streamline email testing processes from our case study with The Software House.
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.
Thank you for reading our Powershell send email to multiple recipients guide, which was originally published in the Mailtrap blog by Zakhar Yung.
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.