How to send an email using a Windows Powershell script
- 48 Comment
Google Query: send email with powershell
In a previous post, I showed you how you can schedule a Powershell script. In this post I show you how to use Powershell to send an email. Thanks to Otto Helweg for the inspiration. This might be useful if you would like an email sent confirming that a script ran successfully.
This can be accomplished by using the Net.Mail.SmtpClient object. The syntax of the Powershell code needed is below:
$emailFrom = "firstname.lastname@example.org"
$emailTo = "email@example.com"
$subject = "your subject"
$body = "your body"
$smtpServer = "your smtp server"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
That’s it. Put this code (filling in your appropriate information) at the end of your Powershell script to send an email after the rest of the script finishes processing. If you use this code snippet in any of your projects please feel free to let me know in the comments.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.