About

MS MVP Logo

View Marc Lognoul [MVP]'s profile on LinkedIn

Disclaimer

The information and materials in this site are provided "AS IS" with no warranties, and confering no rights. This site does not represent the thoughts, intentions, plans or strategies of our employers, customers, friends or family, solely our own personal opinions.

Repost : Sending an SMTP-based mail using PowerShell

by Marc 16. August 2008 19:04

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!

Tags:

PowerShell | Messaging

Comments are closed