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 <>" -To "User02 <>" -Body "Hey User01, how are you doing?" -SmtpServer ""

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 <>" -To "User02 <>" -Body $Body -BodyAsHtml -SmtpServer ""

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 *