Home > Powershell, SQL Server > PowerShell Script to Send Email (multiple recipients)

PowerShell Script to Send Email (multiple recipients)

In this article I will try to wrap how to PowerShell to manage your SQL Server Notifications. And this is very much regular scenario for most of the SQL Server DBA’s to generate notifications when issues arise. There are many ways that we can handle these notifications, but one of the simplest and most reliable and the most common way is email notifications.

We will write simple email function which will have some hard coded values into it. But in a real time scenarios we normally reads these from configuration files such as text file, XML file, ect. I have written a simple function below called SendEmail and this function has tow parameters as subject and the body. This function has From, To email addresses which are hardcoded for now. For multiple recipients we can pass delimited string having valid email addresses.

As we are going to use SMTP relay to send email with PowerShell so we need to provide the information about SMTP Server where I hardcoded about  Hotmail Outgoing Mail Server (SMTP) – smtp.live.com and Port 587 which is the default port for sending secure mail. All these things actually can be dynamic with configuration as I mentioned earlier.

Once all these details are done then created new object cmdlet to create System.Net.Mail.MailMessage object where we can create/configure actual message with from, to email addresses along with subject and body. Then go out and create a net.mail.SMTP client object  with passing two parameters. The first is the name of the SMTP server and the second is the port number we want to use to send the email. Once that’s done we need to set a property of that object that tells us we are certainly going to use SSL or Secure Socket Layers. SSL is just a way to encrypt the email so that when it goes from your computer to the SMTP Servers, nobody can see and read inside of it. Once done need to authenticate the sender credentials to the SMTP server. Finally we are going to call Send method of client object with passing your message as a parameter. That’s it.

Once we are done with the function it’s time to call it by passing parameters to it i.e. Subject and the Body text.

Function SendMail([string]$Subject, [string]$Body)
{
 #pick up these values from configuration
 $SmtpServer = "smtp.live.com"
 $SmtpServerPort = "587"
 $SmtpUser = "FirstName.LastName@domain.com"
 $SmtpPass = "Password"
 $From = "FirstName LastName <FirstName.LastName@domain.com>"

 #assign multiple recipients with delimited string
 [string[]]$To = "firstname.lastname@domain.com,lastname.firstname@yourdomain.com"

 #Create Message Object
 $Message = New-Object System.Net.Mail.MailMessage( $From , $To )
 $Message.Subject = $Subject
 $Message.IsBodyHtml = $true
 $Message.Body = $Body

 #Create SMTP Client Object
 $SMTPClient = New-Object System.Net.Mail.SmtpClient( $SmtpServer , $SmtpServerPort )

 #Enable SSL(Secure Socket Layers) Protocol so email will be sent securly
 $SMTPClient.EnableSsl = $true

 #Create a credential object we will use to authenticate ourselves to the SMTP Server
 $SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $SmtpUser , $SmtpPass );

 #Send Final Message
 $SMTPClient.Send( $Message )
}

#Call the function by passing parameters to it.
SendEmail -Subject "PowerShell Notification" -Body "This is system genrated PowerShell Notification."

So using this is a very simple way to create notifications within your PowerShell scripts.

We will be writing more about PowerShell in coming days stay tune.

Hope this Helps.

Thanks

Sandip

Advertisement
  1. February 18, 2013 at 5:19 am

    Iˇ¦ve read some good stuff here. Definitely price bookmarking for revisiting. I wonder how a lot attempt you set to make this sort of great informative website.

  2. February 24, 2013 at 3:28 pm

    What’s Going down i’m new to this, I stumbled upon this I’ve discovered It positively helpful and it has helped me out loads. I’m hoping
    to give a contribution & aid other customers like
    its helped me. Good job.

  3. February 26, 2013 at 3:35 am

    Thanks for the great article..

  4. March 2, 2013 at 11:41 pm

    It’s essentially a good and helpful piece of information. I am happy that you simply shared this useful information with us. Please keep us up to date similar to this. Thanks for sharing.

  5. March 3, 2013 at 9:25 am

    I simply want to mention I am just very new to blogging and seriously savored your website. Almost certainly I’m want to bookmark your site . You absolutely come with perfect article content. Thanks for sharing your web page.

  6. March 12, 2013 at 4:48 am

    Hello there! This is my first comment here so I just wanted to give a quick shout out and say I genuinely enjoy reading through your articles.

    Can you recommend any other blogs/websites/forums that deal with the
    same topics? Thank you so much!

  7. March 14, 2013 at 5:12 am

    each time i used to read smaller content which
    as well clear their motive, and that is also happening with
    this paragraph which I am reading here.

  8. vps
    March 14, 2013 at 10:11 pm

    This post is amazing. I realy love it!

  9. April 21, 2013 at 2:36 am

    Admiring the dedication you put into your website
    and detailed information you present. It’s awesome to come across a blog every once in a while that isn’t the same old rehashed material.

    Excellent read! I’ve saved your site and I’m adding your RSS feeds to my Google
    account.

  10. July 2, 2013 at 8:33 pm

    Hi, just wanted to say, I liked this post. It was practical.

    Keep on posting!

  11. July 5, 2013 at 4:24 am

    Tremendous things here. I’m very satisfied to peer your post. Thank you a lot and I am taking a look ahead to contact you. Will you please drop me a e-mail?

  12. July 7, 2013 at 1:09 pm

    Very soon this website will be famous among all blog viewers,
    due to it’s nice content

  13. July 24, 2013 at 7:31 am

    Very good post! We are linking to this particularly great post on our website.
    Keep up the great writing.

  14. December 24, 2013 at 1:39 am

    An intriguing discussion is worth comment. I think that you ought to
    write more about this subject, it may not be a taboo matter but typically people
    don’t speak about these issues. To the next!

    Many thanks!!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: