Some times we need to use PS script to send email, script can be used in Task Scheduler or any other program

BatchFile to run script: Create *.bat file

C:WindowsSystem32WindowsPowerShellv1.0powershell.exe -PSConsoleFile "C:Program FilesMicrosoftExchange Serverbinexshell.psc1" -noexit -command ". C:scriptslocationscriptname.ps1"

 

PS Script to send mail: Create  .ps1 file

$filename = “c:filelocationHtmlFIleyouwant torecieve.html
$smtpServer = “SMTP Servername

$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($filename)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = “sendername@domainanme
$msg.To.Add(“recipient_address@domainanme”)
$msg.Subject = “Subject”
$msg.Body = “Body ”
$msg.Attachments.Add($att)
$smtp.Send($msg)