Page 1 of 1

How To Send an E-mail in PowerShell

PostPosted: Wed May 11, 2016 11:33 pm
by admin
Question:
I want to be able to send an e-mail in PowerShell. For example, I got a script which generates a log file. I want an e-mail sent to an administrator with the log file as an attachment. How can I do this in PowerShell ?

Re: How To Send an E-mail in PowerShell

PostPosted: Fri May 13, 2016 11:25 pm
by admin
Answer:
You can use the send-mailmessage cmdlet.

The following is a sample code snippet on how to use the send-mailmessage cmdlet. You can type in get-help send-mailmessage to get the full syntax of the cmdlet.

$emailToAddress="User One<userone@email.com.au>","User Two <usertwo@email.com.au>"
$mymailSubject="Error !!!!!"
$senderAddress= "Some Server<donotreply@email.com.au>"
$message="Please see attached error log."
$mysmtpServer="smtp.email.com.au"
$attachment="c:\myprogram\errorlog.txt"
send-mailmessage -To $emailToAddress -Subject $mymailSubject -From $senderAddress -Body $message -SmtpServer $mysmtpServer -Attachment $attachment