email - Powershell 脚本无法发送给多个收件人

标签 email powershell smtp

我正在使用一个 powershell 脚本,该脚本将创建磁盘空间的 HTML 报告并将其作为电子邮件发送。不幸的是,我无法将脚本发送给多个电子邮件收件人。我正在使用的脚本可以在这里找到:
http://gallery.technet.microsoft.com/scriptcenter/6e935887-6b30-4654-b977-6f5d289f3a63
这是脚本的相关部分......

$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

最佳答案

您使用的 MailMessage 构造函数只需要一个电子邮件地址。请参阅 MSDN 文档
http://msdn.microsoft.com/en-us/library/5k0ddab0.aspx

您应该尝试使用 Send-MailMessage而是因为它是 -To参数接受地址数组
Send-MailMessage -from from@email.mail -To $recipients -Subject "Disk Space Report - $Date" -smptServer smtp.server -Attachments $freeSpaceFileName
注意:Send-MailMessage 是在 PowerShell v2.0 中引入的,这就是为什么仍然有使用其他命令的示例的原因。如果您需要使用 v1.0,那么我会更新我的答案。

关于email - Powershell 脚本无法发送给多个收件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15256862/

相关文章:

php - 通过电子邮件发送错误500而不会向自己发送垃圾邮件

sql-server - 使用 Invoke-Sqlcmd 运行 SQL 脚本以在 Synapse Workspace SQL 池中创建表

rest - 微服务架构中的电子邮件发送

email - 当您想发送高优先级电子邮件时,要设置哪些电子邮件 header ?

php - 如何在访问页面时发送电子邮件通知?

mysql - Powershell - 将日期格式修改为 CSV 列中的值

powershell - 如何在PowerShell中查看可用COM对象的描述列表?

php - SMTP GMAIL 连接

java - 无法连接到 SMTP 端口号 465

c# - .NET SMTP 客户端 - 客户端无权作为此发件人发送