使用PowerShell根据csv文件中的列表将电子邮件发送到不同的地址

时间:2017-02-28 18:58:31

标签: powershell powershell-v5.0

好的,我想我已经删除了所有可识别的信息......

我有56个文件需要去56个不同的人。我正在使用我从这个站点实际获得的PS代码(该问题已关闭)并且大部分工作都很好,但每次运行代码时我收到4条错误消息,但它们似乎影响了输出每次我运行它都不同......

看到我第一次运行它时,有4个文件没有被发送,这是有意义的(4个错误消息),但是当我再次运行它时,我得到相同的4个错误消息,但这次只有3封电子邮件没有通过。最重要的是,这次没有通过的三封电子邮件第一次成功运行(这也意味着第一次没有经历的4次,确实是第二次)。所以不知道如何对此进行路线原因分析:(。任何人有任何建议吗?

PS代码:

$csv = Import-Csv -Delimiter ";" -Path "networkfolder\Dashboard_email.csv" -header "Id","Email"
foreach ($item in $csv) {
$filename = "$($item.id)"
send-mailmessage -to $item.email -attachments $filename -body "Testing <br> Testing" -subject "Testing, may delete after confirming receipt of total count.  I will IM when script has completed." -from myemailaddress@myjob.com -SmtpServer mailrelay.serverlocation.com -cc someone@emailaddress.com
}

PS错误消息:

1

  

send-mailmessage:指定的字符串不是电子邮件地址所需的格式。在networkfolder \ Email testing.ps1:5 char:5 + send-mailmessage -to $ item.email -attachments $ filename -body“We ... + ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo:InvalidType :( :) [Send-MailMessage],FormatException + FullyQualifiedErrorId:FormatException,Microsoft.PowerShell.Commands.SendMailMessage   2

     

send-mailmessage:找不到文件'U:\ Id'。在networkfolder \ Email testing.ps1:5 char:5 + send-mailmessage -to $ item.email -attachments $ filename -body“We ... + ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo:NotSpecified:(:) [Send-MailMessage],FileNotFoundException + FullyQualifiedErrorId:System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage   3

     

send-mailmessage:服务不可用,关闭传输通道。服务器响应是:4.4.1连接超时在Networkfolder \ Email testing.ps1:5 char:5 + send-mailmessage -to $ item.email -attachments $ filename -body“We ... + ~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ + CategoryInfo:InvalidOperation:(System.Net.Mail.SmtpClient:SmtpClient)[Send-MailMessage],SmtpException + FullyQualifiedErrorId:SmtpException,Microsoft.PowerShell.Commands.SendMailMessage   4

     

send-mailmessage:服务不可用,关闭传输通道。服务器响应是:4.4.1连接超时在Networkfolder \ Email testing.ps1:5 char:5 + send-mailmessage -to $ item.email -attachments $ filename -body“We ... + ~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ + CategoryInfo:InvalidOperation:(System.Net.Mail.SmtpClient:SmtpClient)[Send-MailMessage],SmtpException + FullyQualifiedErrorId:SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

如果可能的话,有人能告诉我如何分开段落吗?我读到使用“
”会这样做,但不适合我。

1 个答案:

答案 0 :(得分:0)

尝试在循环中添加Start-Sleep cmdlet。

$csv = Import-Csv -Delimiter ";" -Path "networkfolder\Dashboard_email.csv" -header "Id","Email"
foreach ($item in $csv) {
    $filename = "$($item.id)"
    send-mailmessage -to $item.email -attachments $filename -body "Testing <br> Testing" -subject "Testing, may delete after confirming receipt of total count.  I will IM when script has completed." -from myemailaddress@myjob.com -SmtpServer mailrelay.servername.com -cc someone@emailaddress.com
    Start-Sleep -m 1000
}