简体   繁体   English

用于发送批量电子邮件的PowerShell脚本

[英]PowerShell script for sending bulk emails

I have written a script below that reads the email ID from the XML file. 我在下面编写了一个脚本,该脚本从XML文件读取电子邮件ID。 Loads the email ID filtered using group ID and then send emails to all the email IDs on that ArrayList. 加载使用组ID筛选的电子邮件ID,然后将电子邮件发送到该ArrayList上的所有电子邮件ID。 I don't know what is the problem below, I am not getting any emails. 我不知道下面是什么问题,我没有收到任何电子邮件。

function Test {
   function Send-Mail {
        Param($smtp, $from, $to, $subject, $body)
        $smtp = New-Object System.Net.Mail.SmtpClient($smtp)
        $mail = New-Object System.Net.Mail.MailMessage
        $mail.From = $from
        $mail.To.Add($to)
        $mail.Subject = "subject goes here"
        $mail.Body = "This is test"
        $smtp.Send($mail)
    }
    #Read the xml file and typecast it to System.Xml.XmlDocument
    [Xml]$XmlData = Get-Content Email.xml
    $ArrayEmail = New-Object System.Collections.ArrayList
    $subject = "Testing PowerShell email"
    $from = "a@134.com"
    $smtp = "test.test123.com"
    $body = "The body goes here"
    #get the email list from group id
    $ArrayEmail = $XmlData.emaillist.group |
                  Where-Object {$_.id -eq "IT"} |
                  Select-Object emailid
    $to = New-Object System.Collections.ArrayList
    #store the email ids in array
    for ($i = 0; $i -lt $ArrayEmail.emailid.Count; $i++) {
        $to.Add($ArrayEmail.emailid[$i])
    }
    Send-Mail -Smtp $smtp -From $from -To $to[$i] -Subject $subject -Body $body
 }

Here is my sample XML file: 这是我的示例XML文件:

<emaillist>
  <group id="IT">
    <emailid>a@123.com</emailid>
    <emailid>b@345.com</emailid>
  </group>
  <group id="Admin">
    <emailid>admin@admin.com</emailid>
  </group>
</emaillist>

Not sure what I have done wrong or missed anything as everything seems to be alright. 不知道我做错了什么还是错过了什么,因为一切似乎都还不错。 But I still don't get any emails. 但是我仍然没有收到任何电子邮件。 My smtp is all good, the sender and receivers emails are all good as well. 我的smtp都很好,发送者和接收者的电子邮件也都很好。

What you have there is called off-by-one error . 您所拥有的被称为一次性错误 After your for loop completes the value of the loop variable $i is the highest index of $to plus one . for循环完成后,循环变量$i的值是$to的最高索引加1 Hence $to[$i] evaluates to $null , because you're trying to access the element after the last element in the ArrayList (which doesn't exist), and PowerShell returns $null for things like that instead of throwing an error. 因此, $to[$i]计算结果为$null ,因为您正尝试访问ArrayList中最后一个元素(不存在) 之后的元素,PowerShell会为类似的事情返回$null而不是抛出错误。

Replace this: 替换为:

$ArrayEmail = $XmlData.emaillist.group |
              Where-Object {$_.id -eq "IT"} |
              Select-Object emailid
$to = New-Object System.Collections.ArrayList
#store the email ids in array
for ($i = 0; $i -lt $ArrayEmail.emailid.Count; $i++) {
    $to.Add($ArrayEmail.emailid[$i])
}
Send-Mail -Smtp $smtp -From $from -To $to[$i] -Subject $subject -Body $body

with this: 有了这个:

$to = $XmlData.emaillist.group |
      Where-Object {$_.id -eq "IT"} |
      Select-Object -Expand emailid
Send-Mail -Smtp $smtp -From $from -To $to -Subject $subject -Body $body

and the problem will disappear. 问题就会消失。


To have the mail sent to all recepients you need to add them one by one: 要将邮件发送给所有收件人,您需要一个一个地添加它们:

function Send-Mail {
    Param($smtp, $from, $to, $subject, $body)
    $smtp = New-Object System.Net.Mail.SmtpClient($smtp)
    $mail = New-Object System.Net.Mail.MailMessage
    $mail.From = $from
    $to | ForEach-Object { $mail.To.Add($_) }
    $mail.Subject = "subject goes here"
    $mail.Body = "This is test"
    $smtp.Send($mail)
}

However, rather than re-invent the wheel you should use the Send-MailMessage cmdlet, as others have already suggested: 但是,您应该使用Send-MailMessage cmdlet,而不是重新发明轮子,就像其他人已经建议的那样:

Send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Body $body

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM