Powershell : Send a HTML message

If you would like to send a HTML message rather than a plain text one, this is a script I use to complete this action, simple but a reference for others, this saves getting the HTML content correct in PowerShell where its easy to make a typo.

#####Define Variables#####
$fromaddress = “mail@bears.local”
$toaddress = “GiveMeBears@bears.local”
$Subject = “List of all the bears”
$body = Get-Content "c:\bearlist\message.html"
$attachment = "c:\bearlist\attachment.xls"
$smtpserver = “mailmethegrizzle.bear.local”

##################################

$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)

####################################################
Previous Post Next Post

نموذج الاتصال