Dec 5 2007

How to send an email using a Windows Powershell script

If you're new here, you may want to start with my most popular posts. Then, subscribe to my RSS feed to stay updated. Thanks for visiting!

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 = "user@yourdomain.com"
$emailTo = "user@yourdomain.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.

Happy Coding!

Technorati Tags: , , ,

Related posts:

  1. How to put line breaks into a text string in Windows Powershell
  2. How to schedule a Windows Powershell script
  3. How to deal with long filenames in Windows Powershell

TAGS:

7 Comments on this post

Trackbacks

  1. Emails e Powershell « FreeBeing wrote:
  1. Ivan Versluis said:

    I used this example to schedule a ps1 script and email the output. thanks again!

    December 15th, 2007 at 2:19 pm
  2. hstagner said:

    Hello Ivan,

    I am glad that the example worked for you and it was something that you could use in one of your scripts.

    Thanks for reading!

    Regards,

    Harley Stagner

    December 17th, 2007 at 11:00 am
  3. Leon said:

    Hello Harley

    I use scom2007 and in the monitor console i would like to select an alert and then mail it to our automated ticket system. In the mailbody i should use several parameters from the alert.

    any idea how to handle this? can i use you’re script within the monitor console

    grtz
    Leon

    July 2nd, 2008 at 4:05 pm
  4. hstagner said:

    Hello Leon,

    I have not used scom2007 all that much. However, I do know that it is powershell-enabled. So, you should be able to use the code here with it. I think you will find the information at:

    http://dmitrysotnikov.wordpress.com/2007/03/21/momscom-powershell/

    Useful. Thank you for reading!

    Regards,

    Harley Stagner

    July 2nd, 2008 at 11:28 pm
  5. Nitin Arora said:

    How would you add an attachment?
    Definition : System.Void Send(String from, String recipients, String subject, String body), System.Void Send(MailMessage message)

    In VBScript we can use the CDO.Message object and use the method AddAttachment.

    August 12th, 2008 at 11:45 am
  6. hstagner said:

    Hello Nitin,

    Thank you for reading! I found your answer at Grant Holliday’s Blog

    Here is the code:

    $filename = “logfile.txt”
    $smtpServer = “localhost”

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

    $msg.From = “somebody@yourdomain.com”
    $msg.To.Add(”somebody@theirdomain.com”)
    $msg.Subject = “Nightly Log File”
    $msg.Body = “The nightly log file is attached”
    $msg.Attachments.Add($att)

    $smtp.Send($msg)

    Thanks Grant!

    August 17th, 2008 at 6:50 pm

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

Recent Readers

JOIN MY COMMUNITY!
                 
                  Computers Blogs - Blog Top Sites