How To Send an E-mail in PowerShell

How To Send an E-mail in PowerShell

Postby admin » Wed May 11, 2016 11:33 pm

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 ?
admin
Site Admin
 
Posts: 106
Joined: Mon Jan 18, 2010 11:05 am

Re: How To Send an E-mail in PowerShell

Postby admin » Fri May 13, 2016 11:25 pm

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
admin
Site Admin
 
Posts: 106
Joined: Mon Jan 18, 2010 11:05 am


Return to Powershell

Who is online

Users browsing this forum: No registered users and 2 guests

cron