Marc Lognoul's IT Infrastructure Blog

Cloudy with a Chance of On-Prem


PowerShell: Sending an SMTP Mail (With a goodie)

Nothing new here, just a repost by popular demand… 

We need an SMTP Client object and a Mail Message object:

$SmtpClient = new-object system.net.mail.smtpClient

$MailMessage = New-Object system.net.mail.mailmessage

Then a basic configuration of their properties (you may have to adapt to fit you environment’s needs)

$SmtpClient.Host = “mysmtpserver.mycompany.local”

$mailmessage.from = “notification-services@mycompany.local”

$mailmessage.To.add(“itadmins@mycompany.local”)

$mailmessage.Subject = “Daily Uptime Reports”

$mailmessage.Body = “Server01. mycompany.local: 99.999% uptime”

And now, the special touch that makes Outlook display the message with a “bell” icon à la SharePoint notification:

$mailmessage.Headers.Add(“message-id”, “<3BD50098E401463AA228377848493927-1>”) 

All you need is to add 3BD50098E401463AA228377848493927 before the actual message ID and separate them with a dash  And then send the message

$smtpclient.Send($mailmessage)

If you want your email to look even more eye-candy, enabled and add HTML to the body before sending

$mailmessage.IsBodyHtml = 1

You can even read from a file using get-content and generate the body

$mailmessage.Body = Get-Content .report.html

Find more information over here:

And cut!