Powershell script unable to send to multiple recipients
The MailMessage constructor you are using only takes one email address. See the MSDN documentation http://msdn.microsoft.com/en-us/library/5k0ddab0.aspx
You should try using Send-MailMessage
instead because it's -To
parameter accepts an array of addresses
Send-MailMessage -from [email protected] -To $recipients -Subject "Disk Space Report - $Date" -smptServer smtp.server -Attachments $freeSpaceFileName
Note: Send-MailMessage was introduced in PowerShell v2.0 so that's why there are still examples that use other commands. If you need to use v1.0, then I will update my answer.
There are two methods to send emails using PowerShell:
For the
Send-MailMessage
method (Introduced in PowerShell Version 2):$to = "[email protected]", "[email protected]"
For the
System.Net.Mail
method (Works from PowerShell Version 1):$msg.To.Add("[email protected]")
$msg.To.Add("[email protected]")
With System.Net.Mail
you can also do this in one line. Just be sure to add the parentheses and all recipients comma-separated within a single string:
$msg = New-Object System.Net.Mail.MailMessage("[email protected]","[email protected],[email protected]","Any subject,"Any message body")
This also works for RFC-822 formatted e-mail addresses:
System.Net.Mail.MailMessage("Sender <[email protected]>","Rcpt1 <[email protected]>,Rcpt2 <[email protected]>","Any subject,"Any message body")