Powershell script failed to send to multiple recipients

I am using a powershell script that will create an HTML report on disk space and send it as an email. Unfortunately, I cannot get a script to send to more than one email recipient. The script I'm using can be found here:

http://gallery.technet.microsoft.com/scriptcenter/6e935887-6b30-4654-b977-6f5d289f3a63

Here are the relevant parts of the script ...

$freeSpaceFileName = "FreeSpace.htm" $serverlist = "C:\sl.txt" $warning = 90 $critical = 75 New-Item -ItemType file $freeSpaceFileName -Force Function sendEmail { param($from,$to,$subject,$smtphost,$htmlFileName) $body = Get-Content $htmlFileName $smtp= New-Object System.Net.Mail.SmtpClient $smtphost $msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body $msg.isBodyhtml = $true $smtp.send($msg) } $date = ( get-date ).ToString('yyyy/MM/dd') $recipients = " to1@email.com ", " to2@email.com " sendEmail from@email.mail $recipients "Disk Space Report - $Date" smtp.server $freeSpaceFileName 

I get the following error

 New-Object : Exception calling ".ctor" with "4" argument(s): "The specified string is not in the form required for an e -mail address." At E:\TRIRIGA\dps_jobs\DiskSpaceReport.ps1:129 char:18 + $msg = New-Object <<<< System.Net.Mail.MailMessage $from, $to, $subject, $body + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand 
+4
source share
5 answers

The MailMessage constructor that you use takes only one email address. See MSDN Documentation http://msdn.microsoft.com/en-us/library/5k0ddab0.aspx

You should try instead of Send-MailMessage , because the -To parameter accepts an array of addresses

Send-MailMessage -from from@email.mail -To $recipients -Subject "Disk Space Report - $Date" -smptServer smtp.server -Attachments $freeSpaceFileName

Note. Send-MailMessage was introduced in PowerShell v2.0, so why are there more examples of using other commands. If you need to use v1.0, I update my answer.

+7
source

There are two ways to send email using PowerShell:

  1. For the Send-MailMessage (introduced in PowerShell version 2):

    $to = " to1@email.com ", "to 2@email.com "

  2. For the System.Net.Mail method (works with PowerShell Version 1):

    $msg.To.Add(" to1@email.com ")

    $msg.To.Add(" to2@email.com ")

+6
source

With System.Net.Mail you can also do this in one line. Be sure to add the brackets and all recipients separated by commas in one line:

 $msg = New-Object System.Net.Mail.MailMessage(" from@email.com "," to@email1.com , to@email2.com ","Any subject,"Any message body") 

This also works for email addresses in RFC-822 format:

 System.Net.Mail.MailMessage("Sender < from@email.com >","Rcpt1 < to@email1.com >,Rcpt2 < to@email2.com >","Any subject,"Any message body") 
+2
source

try the following:

 Function sendEmail { param($from,[string[]]$to,$subject,$smtphost,$htmlFileName) $body = Get-Content $htmlFileName $smtp= New-Object System.Net.Mail.SmtpClient $smtphost $msg = New-Object System.Net.Mail.MailMessage $msg.from =$from foreach($a in $to) { $msg.to.Add($a) } $msg.Subject= $subject $msg.Body = $body $msg.isBodyhtml = $true $smtp.send($msg) } sendemail -from from@email.mail -to $recipients -smtphost smtp.server -subject "Disk Space Report - $Date" -htmlFileName $freeSpaceFileName 
0
source

I recommend using send-mailmessage in powershell instead of defining your own function. I assume that you have a type mismatch on one of your parameters.

0
source

All Articles