使用PowerShell发送多个相同的邮件

时间:2017-02-04 03:16:53

标签: powershell

是否可以向同一个用户发送多封powershell个邮件,例如10封邮件? 使用跟随脚本我只能发送邮件。

$From ="user name"
$To = "user name"
$Subject = "Email Subject"
$Body = "hello"
$`SMTPServer` = "`smtp` name"
$`SMTPPort` = "`nrport`"
$BCC = "`username`"
Send-`MailMessage` -From $From -to $To  -Subject $Subject -Body $Body -`SmtpServer` $`SMTPServer`  -`UseSsl` -Bcc $BCC

3 个答案:

答案 0 :(得分:0)

只需使用for循环多次执行某些操作(在本例中为10):

for ($i = 1; $i -le 10; $i++)

这是非常普遍的概念,并非PowerShell独有的

答案 1 :(得分:0)

尝试类似这样的事情

$From ="user name"
$To = "user name"
$SMTPServer = "smtp name"
$SMTPPort = "nrport"

$ListMailSubsject=@([pscustomobject]@{subject="subject1"; body="body1"}, [pscustomobject]@{subject="subject1"; body="body2"})

foreach ($MailSubsject in $ListMailSubsject)
{
   Send-MailMessage -From $From -to $To  -Subject $MailSubsject.subject -Body $MailSubsject.body -SmtpServer $SMTPServer  -UseSsl -Bcc $BCC  
}

答案 2 :(得分:0)

您可以使用以下代码。尝试使用" splatting"。它使代码更具可读性和清晰度。

1..10 | foreach-object {
    $mail = @{
        From ="user name"
        To = "user name"
        Subject = "Email Subject"
        Body = "hello"
        SMTPServer = "smtp name"
        SMTPPort = "port"
        BCC = "username"
        }
    Send-MailMessage @mail
}