Monday 20 January 2020

PowerShell to Send Mail in HTML format using SMTP

PowerShell Script to Send mail in html format using smtp Server:

PS Script to Send mail with attachment(optional):
Information required:
1. Create ".html page" like in script using "content.html"
2. Make a note of file path, if needs to be attached with an email.
3. smtp server details.
4. Save the below script somewhere with ".ps1" extension, That's it!!

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

###########Define Variables########

$fromaddress = "donotreply@labtest.com"
$toaddress = "A@labtest.com"
$bccaddress = "A@labtest.com"
$CCaddress = "A@labtest.com"
$Subject = "ACtion Required"
$body = get-content C:\dba_path\content.html
$attachment = "C:\dba_path\test.txt"
$smtpserver = "smtp.lab.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)

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

No comments:

Post a Comment