Monday, October 21, 2013

Poweshell Send-MailMessage, sending an e-mail in one line of code

In powershell 2.0 you would of had to write function to send an e-mail from powershell. Lucky for us in Powershell 3.0 it's

Poweshell 3.0 
Send-MailMessage -To $Mailto -From "" -Subject "XYZ" -SmtpServer "XYZ" -Body "TEST" -Attachments XYZ.csv

Powershell 2.0
 function sendMail{
     Write-Host "Sending Email"
     #SMTP server name
     $smtpServer = "smtpXXXX"
     #Creating a Mail object
     $msg = new-object Net.Mail.MailMessage
     #Creating SMTP server object
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)
     $att = new-object Net.Mail.Attachment($OUTfile)
     #Email structure
     $msg.ReplyTo =""
     $msg.subject = "TEST"
     $msg.body = "This is the email Body."
     #Sending email

I like the Powershell 3.0 version better.

If you find this article useful please leave me a comment or click on an ad to show your support.

No comments :

Post a Comment