Send e-mail using Powershell

Hey there,

so today I’d like to take a look at sending an e-mail from Powershell, that I’m sure many of us had to do.

In Powershell v 2.0 and above, there is a nice cmdlet Send-MailMessage. Let’s have a look and the syntax:

Send-MailMessage [-Attachments <String[]>] [-Bcc <String[]>] [[-Body] <String>] [-BodyAsHtml]
 [-Encoding <Encoding>] [-Cc <String[]>] [-DeliveryNotificationOption <DeliveryNotificationOptions>]
 -From <String> [[-SmtpServer] <String>] [-Priority <MailPriority>] [-Subject] <String> [-To] <String[]>
 [-Credential <PSCredential>] [-UseSsl] [-Port <Int32>] [<CommonParameters>]

And now a bit of an example:

Send-MailMessage -From "User01 <user01@example.com>" -To "User02 <user02@example.com>" -Body "Hey User01, how are you doing?" -SmtpServer "smtp.example.com"

We can also send HTML body e-mails with following syntax:

$Body = @'
<strong>Hey User01,</strong>
I hope you are doing fine!
'@

Send-MailMessage -From "User01 <user01@example.com>" -To "User02 <user02@example.com>" -Body $Body -BodyAsHtml -SmtpServer "smtp.example.com"

That’s it for today. I hope you find it useful.

Enjoy the rest of the day!

Robot(ICT) guy

Published by

Lukas Vu

I started in IT with my own business since high school providing automated and centralized hosting solutions to end customers. Nowadays I'm focusing on corporate area, analyzing, managing and improving customer's IT environments. My main focus nowadays is automation and architectural improvements.

Leave a Reply

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