Link to home
Start Free TrialLog in
Avatar of Brian Sandt
Brian Sandt

asked on

Help with emailing multiple people using powershell

I need to email a file to multiple recipients.  I have been attempting powershell but I keep getting errors saying my parameter 'To' is an empty string.
Here is what I've got:
Function Send-MailMessage {
Param (
[Parameter(`
Mandatory=$true)]
[String]$To,
[Parameter(`
Mandatory=$true)]
[String]$Subject,
[Parameter(`
Mandatory=$true)]
[String]$Body,
[Parameter(`
Mandatory=$true)]
[String]$Password,
[Parameter(`
Mandatory=$true)]
[String]$Attachment
)

$EmailFrom = "FACSRowCount@hbcs.org"
$recipients = @('email1@abc.com', 'email2@abc.com')
$SMTPServer = "mail.abc.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$Att = New-Object System.Net.Mail.Attachment($Attachment)
$SMTPMessage.Attachments.add($Att)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($EmailFrom, $Password);
$SMTPClient.Send($SMTPMessage)
Remove-Variable -Name SMTPClient
Remove-Variable -Name Password

} #End
Send-MailMessage -To $recipients -Subject "Subject" -SmtpServer $SMTPServer -From $EmailFrom -Attachment "c:\abc.txt"

Error:
Send-MailMessage : Cannot bind argument to parameter 'To' because it is an empty string.
+ Send-MailMessage -To $recipients -Subject "Subject" -Smt ...
+                      ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Send-MailMessage
ASKER CERTIFIED SOLUTION
Avatar of Kimputer
Kimputer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Without Set-StrictMode defining is not required
User generated imageYou do not want to loop recipients because that will generate multiple emails instead of one