Powershell 脚本,它以较小的批次读取文本文件。

Powershell Script which reads text files in smaller batches.

我在向大约多个电子邮件地址发送 SMTP 电子邮件时遇到了问题。 200. 我正在寻找一个脚本,它在其中读取包含 200 个电子邮件地址的 .txt 文件,并以较小的批次使用以下脚本发送通用 SMTP 消息。

发送通用电子邮件的脚本如下:

$to = "TO EMAIL"
$smtp = "SMTP Server"
$from = "FROM EMAIL"
$subject = "Subject" 
$body = "EMAIL BODY"
send-MailMessage -SmtpServer $smtp -To $to -Bcc (Get-Content "\FILE Location") -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high

如有任何帮助,我们将不胜感激。

这是一种解决方案(可能有 other/better 种方法):

$to = "TO EMAIL"
$smtp = "SMTP Server"
$from = "FROM EMAIL"
$subject = "Subject" 
$body = "EMAIL BODY"

$Recipient = Get-Content "emails.txt"
$NumberOfBatches = [int]($Recipient.count / 50)

For ($i = 0; $i -lt $NumberOfBatches; $i++) { 
    $Emails = $Recipient | Select -First 50 -Skip ($i * 50)
    Send-MailMessage -SmtpServer $smtp -To $to -Bcc $Emails -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high
}

这会将电子邮件地址列表加载到名为 $Recipient 的变量中。

然后计算一次发送 50 封电子邮件需要多少批次,将其转换为 [int] 以获得整数。

然后使用 For 循环执行定义的批次数量,并使用 Select-Object cmdlet 按定义的批次过滤电子邮件列表,方法是使用 -First-Skip.