Hi Readers,

Sharing a sweet code to send HTML email via powershell.

Aattched is a very simple method for sending the html email.

zip file contains script & html file (you can edit in MS word according to your requirment).

Most of the parameters that  are required to send email are included, you can use what ever you require.

Extract the zip file & Check the sample.

If you search on Internet, you will find complex method in which scripters have used html code inside the script.

I have used a very simple method by creating the word file & saving it as html.

 

Script also sends attachment, if you want to send attachment then change the path accordingly in variables.(# Hash the things that you dont require in your script email)

 

follow me :- http://msexchange.me

 

PowerShell
Edit|Remove
############################################################################### 
 
###########Define Variables######## 
 
$fromaddress = "donotreply@labtest.com" 
$toaddress = "Aishwarya.Rawat@labtest.com" 
$bccaddress = "Vikas.sukhija@labtest.com" 
$CCaddress = "Mahesh.Sharma@labtest.com" 
$Subject = "ACtion Required" 
$body = get-content .\content.htm 
$attachment = "C:\sendemail\test.txt" 
$smtpserver = "smtp.labtest.com" 
 
#################################### 
 
$message = new-object System.Net.Mail.MailMessage 
$message.From = $fromaddress 
$message.To.Add($toaddress$message.CC.Add($CCaddress$message.Bcc.Add($bccaddress$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) 
 
#################################################################################