Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Email Powershell脚本无法发送到多个收件人_Email_Powershell_Smtp - Fatal编程技术网

Email Powershell脚本无法发送到多个收件人

Email Powershell脚本无法发送到多个收件人,email,powershell,smtp,Email,Powershell,Smtp,我正在使用一个powershell脚本,它将创建一个关于磁盘空间的HTML报告,并将其作为电子邮件发送。不幸的是,我无法将脚本发送给多个电子邮件收件人。我使用的脚本可以在这里找到: 以下是脚本的相关部分 $freeSpaceFileName = "FreeSpace.htm" $serverlist = "C:\sl.txt" $warning = 90 $critical = 75 New-Item -ItemType file $freeSp

我正在使用一个powershell脚本,它将创建一个关于磁盘空间的HTML报告,并将其作为电子邮件发送。不幸的是,我无法将脚本发送给多个电子邮件收件人。我使用的脚本可以在这里找到:

以下是脚本的相关部分

$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
我得到以下错误

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
New Object:Exception调用带有“4”参数的“.ctor”:“指定字符串的格式不是e
-邮寄地址。”
在E:\DiskSpaceReport.ps1:129 char:18
+$msg=新建对象尝试以下操作:

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

我建议在powershell中使用send mailmessage,而不是定义自己的函数。我猜您的一个参数的类型不匹配。

您使用的MailMessage构造函数只使用一个电子邮件地址。请参阅MSDN文档

您应该尝试改用
发送邮件
,因为它的
-To
参数接受一个地址数组

发送邮件消息-来自from@email.mail-至$recipients-主题为“磁盘空间报告-$Date”-smptServer smtp.server-附件$freeSpaceFileName


注意:Send MailMessage是在PowerShell v2.0中引入的,因此仍然有使用其他命令的示例。如果您需要使用v1.0,那么我将更新我的答案。

使用PowerShell发送电子邮件有两种方法:

  • 对于
    发送邮件消息
    方法(在PowerShell版本2中引入):

    $to=”to1@email.com", "to2@email.com“

  • 对于
    System.Net.Mail
    方法(适用于PowerShell版本1):

    $msg.To.Add(“to1@email.com“”

    $msg.To.Add(“to2@email.com“”


  • 使用
    System.Net.Mail
    也可以在一行中完成此操作。只需确保在单个字符串中添加括号和逗号分隔的所有收件人:

    $msg = New-Object System.Net.Mail.MailMessage("from@email.com","to@email1.com,to@email2.com","Any subject,"Any message body")
    
    这也适用于RFC-822格式的电子邮件地址:

    System.Net.Mail.MailMessage("Sender <from@email.com>","Rcpt1 <to@email1.com>,Rcpt2 <to@email2.com>","Any subject,"Any message body")
    
    System.Net.Mail.MailMessage(“发件人”、“Rcpt1、Rcpt2”、“任何主题”、“任何邮件正文”)
    
    我尝试了两个不同的电子邮件地址。当我将$recipients设置为其中一个地址时,它可以工作。但是,一旦我将它们都合并到$recipients列表中,它就会失败。@GeoffDawdy,是的。.我发布了一个新的解决方案!在powershell v3.0上,我没有错误,但从未发送给第二个收件人的电子邮件。这样就正确填充了$msg.to。