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 (17)

  1. Sudhakar says:

    Will this work without "ADD-PSSnapin" of Microsoft exchange??

  2. Reader says:

    OK … now how do we get it to send text from a file, instead of a fixed string?

  3. Reply says:

    $msg.body = Get-Content C:body.txt

  4. Jimesh Makawana says:

    Sending Email

    Exception calling "Send" with "1" argument(s): "The SMTP server requires a secure connection or the client was not authenticated. The server response was: Authentication required"

    At line:24 char:16

    +      $smtp.Send <<<< ($msg)

       + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

       + FullyQualifiedErrorId : DotNetMethodException

  5. DBT says:

    Or you can use the cmd-let

    #$cred = get-credential

    send-mailMessage -to "" -subject "test" -from "" -body "" -SmtpServer "" #-credential $cred

  6. TK says:

    Hi, one question, how can i load mail body from html file?

  7. John Wilkinson says:

    Is there a way to get a confirmation message or mail read message from the command line?

  8. Caveman says:

    Pretty helpfull this one, you can also find an handson example right here:

    http://www.powershellcave.com

    Helped me implement the mail functions into reporting scripts and such.

  9. Hassan, says:

    how i can add option authentication ?

  10. new to powershell says:

    Would someone please explain in detail how to send email using window 7 Powershell to this "interpreter411@gmail.com"

  11. Gaurav says:

    This script doesn't work for me… can you please help..?

    Thanks

    Gaurav

  12. Rob says:

    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?

  13. Kim says:

    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?

  14. $msg.ReplyTo  Doesn't work with multiple recipient , please let me know if it is known

  15. selva says:

    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

  16. SEBASTIAN NATHAN says:

    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