Share via


Sending email using Powershell script

 

 Below is a script that can be used to send email using Powershell.

 function sendMail{

     Write-Host "Sending Email"

     #SMTP server name
     $smtpServer = "smtp.xxxx.com"

     #Creating a Mail object
     $msg = new-object Net.Mail.MailMessage

     #Creating SMTP server object
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)

     #Email structure
     $msg.From = "fromID@xxxx.com"
     $msg.ReplyTo = "replyto@xxxx.com"
     $msg.To.Add("toID@xxxx.com")
     $msg.subject = "My Subject"
     $msg.body = "This is the email Body."

     #Sending email
     $smtp.Send($msg)
 
}

#Calling function
sendMail

Comments

  • Anonymous
    April 26, 2012
    Thanks buddy . Useful Tip

  • Anonymous
    July 15, 2012
    Will this work without "ADD-PSSnapin" of Microsoft exchange??

  • Anonymous
    July 18, 2012
    OK ... now how do we get it to send text from a file, instead of a fixed string?

  • Anonymous
    July 30, 2012
    $msg.body = Get-Content C:body.txt

  • Anonymous
    September 13, 2012
    The comment has been removed

  • Anonymous
    October 09, 2012
    Or you can use the cmd-let #$cred = get-credential send-mailMessage -to "" -subject "test" -from "" -body "" -SmtpServer "" #-credential $cred

  • Anonymous
    April 28, 2013
    Hi, one question, how can i load mail body from html file?

  • Anonymous
    May 16, 2013
    The comment has been removed

  • Anonymous
    June 03, 2013
    Pretty helpfull this one, you can also find an handson example right here: www.powershellcave.com Helped me implement the mail functions into reporting scripts and such.

  • Anonymous
    October 21, 2013
    how i can add option authentication ?

  • Anonymous
    February 20, 2014
    Would someone please explain in detail how to send email using window 7 Powershell to this "interpreter411@gmail.com"

  • Anonymous
    February 23, 2014
    This script doesn't work for me... can you please help..? Thanks Gaurav

  • Anonymous
    February 25, 2014
    how would you add multiple to addresses from a text file? Like this $msg.To.Add(Get-Content <path to text> And would the text file need to be comma or semicolon separated, or have addresses on their own lines?

  • Anonymous
    May 19, 2014
    Do you have to have Outlook installed to send an email or will it just use the server info?  I'm trying to send the email from a server.  I want the scripts to run before I get in and boot up, so I set them up on the server.  They will report to a file but I'd like them emailed to a group.  The email doesn't go out  but it works form my PC.  I think it must have Outlook installed or use OWA.  Am I correct?

  • Anonymous
    July 23, 2014
    $msg.ReplyTo  Doesn't work with multiple recipient , please let me know if it is known

  • Anonymous
    December 15, 2014
    I got the below msg when run the script.pls help me to rectify it Exception calling "Send" with "1" argument(s): "Failure sending mail." At C:MailSendingMail.ps1:22 char:16

  •      $smtp.Send <<<< ($msg)    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException    + FullyQualifiedErrorId : DotNetMethodException
  • Anonymous
    April 29, 2015
    HAVEING 28 YEARS EXPEREINCE IN OIL AND GAS PROJECST AT ON/OFFSHORE/MARINE SHIPPING WITH BLASTING/WET SPONGE BLASTING/HYDOR JETTING/COATING/INSLATION/FIREPROOFING/SCAFFOLDING/INSPECTION