Sending email using Powershell script

Sending email using Powershell script

Rate This
  • Comments 14

 

 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
  • Thanks buddy . Useful Tip

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

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

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

  • 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

  • Or you can use the cmd-let

    #$cred = get-credential

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

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

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

  • 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.

  • how i can add option authentication ?

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

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

    Thanks

    Gaurav

  • 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?

  • 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?

Page 1 of 1 (14 items)
Leave a Comment
  • Please add 8 and 7 and type the answer here:
  • Post