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 <firstname.lastname@example.org>" -To "User02 <email@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 <firstname.lastname@example.org>" -To "User02 <email@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!