Sending an email using PowerShell

Sending an email using PowerShell

PowerShell is proving to be a wonderful tool for Microsoft System Admins. The ability to send emails in a PowerShell script can be quite useful, you can use this simple little function to send an email from PowerShell.

You could potentially use this function to send you an email when an automatic PowerShell task runs and fails or if you want to notify yourself of something during the script.

The results of my test are below the PowerShell code.
Function SendMail{
#Set your outgoing SMTP Server, this will work as long as your server does not need authentication
$MailServer = "mail.bigpond.com" #Change this to our SMTP server
#Creating a Mail object
$MailObject = new-object Net.Mail.MailMessage
#Creating SMTP server object
$SMTP = new-object Net.Mail.SMTPClient($MailServer)
#Actual email text
$MailObject.From = "[email protected]"
$MailObject.ReplyTo = "[email protected]"
$MailObject.To.Add("[email protected]")
$MailObject.subject = "This is the test email"
$MailObject.body = "Hello," + "`r`n" + `
"Powershell is sending you an email"
#Send the email
$SMTP.Send($MailObject)
}

send email powershell script

Email received from my test
Email received from my test sending with PowerShell, it got filtered into spam but it worked!!
Replies: 0 / Share:

You might also like …

Post Comment

Your email address will not be published. Required fields are marked *