Reputation: 906
I'm using a powershell script that will create an HTML report of disk space and send it as an email. Unfortunately I'm unable to get the 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 = "[email protected]", "[email protected]"
sendEmail [email protected] $recipients "Disk Space Report - $Date" smtp.server $freeSpaceFileName
I'm getting 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:\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
Upvotes: 7
Views: 35402
Reputation: 21
Looks like MailMessage constructor had been fixed by Microsoft as it accepts multiple recipients, separated by comma character (",") according to https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage.-ctor?redirectedfrom=MSDN&view=net-5.0#System_Net_Mail_MailMessage__ctor_System_String_System_String_System_String_System_String_
to String - A String that contains the addresses of the recipients of the email message. Multiple email addresses must be separated with a comma character (",").
Upvotes: 2
Reputation: 4265
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]")
Upvotes: 9
Reputation: 71
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")
Upvotes: 6
Reputation: 60910
try this:
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 [email protected] -to $recipients -smtphost smtp.server -subject "Disk Space Report - $Date" -htmlFileName $freeSpaceFileName
Upvotes: 1
Reputation: 5241
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.
Upvotes: 9
Reputation: 1
I recommend using send-mailmessage in powershell instead of defining your own function. My guess is you have a type mismatch on one of your parameters.
Upvotes: 0