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

Using PowerShell to send email to different addresses based on list in a csv file

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

我有 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 : The specified string is not in the form required for an e-mail address. At 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 : Could not find file 'U:\Id'. At 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 : Service not available, closing transmission channel. The server response was: 4.4.1 Connection timed out At 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 : Service not available, closing transmission channel. The server response was: 4.4.1 Connection timed out At 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

另外,如果可以的话,谁能告诉我如何分隔段落?我读到使用“
”可以做到这一点,但对我不起作用。

尝试在循环中添加 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
}