Powershell在Excel电子表格中向收件人发送电子邮件

Powershell在Excel电子表格中向收件人发送电子邮件,powershell,email,Powershell,Email,我的电子表格位于C:\scripts\test.csv,其中包含用户电子邮件地址列表 我正试图将下面的电子邮件发送给这些用户中的每一位,但它只发送给其中一位用户。如何将其发送给所有用户 $recipients = get-content C:\scripts\test.csv $smtpServer = "mail.server.com" $msg = new-object Net.Mail.MailMessage $smtp = new-object Net.Mail.SmtpClient(

我的电子表格位于C:\scripts\test.csv,其中包含用户电子邮件地址列表

我正试图将下面的电子邮件发送给这些用户中的每一位,但它只发送给其中一位用户。如何将其发送给所有用户

$recipients = get-content C:\scripts\test.csv

$smtpServer = "mail.server.com"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "FromUser@email.com"
$msg.To.Add($recipients)
$msg.subject = "Requiring Updates"
$msg.IsBodyHTML = $true
$body = @'
<html> 
  <body> 
    <font face="calibri">Hello, please read this test email.</font>
  </body> 
</html> 
'@ 

$msg.body = $body
$smtp.Send($msg)
$recipients=获取内容C:\scripts\test.csv
$smtpServer=“mail.server.com”
$msg=新对象Net.Mail.MailMessage
$smtp=新对象Net.Mail.SmtpClient($smtpServer)
$msg.From=”FromUser@email.com"
$msg.To.Add($recipients)
$msg.subject=“需要更新”
$msg.IsBodyHTML=$true
$body=@'
您好,请阅读此测试电子邮件。
'@ 
$msg.body=$body
$smtp.Send($msg)

因此您必须在foreach循环中逐个迭代它们

将现有代码更改为:

$recipients = get-content C:\scripts\test.csv


foreach($rcpt in $recipients)
{
$smtpServer = "mail.server.com"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "FromUser@email.com"
$msg.To.Add($rcpt)
$msg.subject = "Requiring Updates"
$msg.IsBodyHTML = $true
$body = @'
<html> 
  <body> 
    <font face="calibri">Hello, please read this test email.</font>
  </body> 
</html> 
'@ 

$msg.body = $body
$smtp.Send($msg)

}
$recipients=获取内容C:\scripts\test.csv
foreach($rcpt,单位为$recipients)
{
$smtpServer=“mail.server.com”
$msg=新对象Net.Mail.MailMessage
$smtp=新对象Net.Mail.SmtpClient($smtpServer)
$msg.From=”FromUser@email.com"
$msg.To.Add($rcpt)
$msg.subject=“需要更新”
$msg.IsBodyHTML=$true
$body=@'
您好,请阅读此测试电子邮件。
'@ 
$msg.body=$body
$smtp.Send($msg)
}

希望有帮助。

这是新的答案,因为您的需求现在发生了变化

将emailid直接一个接一个地放在csv文件中,不加引号,因为我已经附加了引号。现在,这将向读取文件的所有收件人发送一封电子邮件

$recipients = get-content C:\scripts\test.csv

Remove-Variable rcpt -ErrorAction Ignore

Remove-Variable result -ErrorAction Ignore

Remove-Variable final_result -ErrorAction Ignore
foreach($rcpt in $recipients)
{

$result += "'$rcpt'" + ","
$final_result=$result.Trim(',')

$smtpServer = "mail.server.com"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "FromUser@email.com"
$msg.To.Add($final_result)
$msg.subject = "Requiring Updates"
$msg.IsBodyHTML = $true
$body = @'
<html> 
  <body> 
    <font face="calibri">Hello, please read this test email.</font>
  </body> 
</html> 
'@ 

$msg.body = $body


}
$smtp.Send($msg)  
$recipients=获取内容C:\scripts\test.csv
删除变量rcpt-错误操作忽略
删除变量结果-ErrorAction忽略
删除变量最终结果-错误操作忽略
foreach($rcpt,单位为$recipients)
{
$result+=“$rcpt”“+”,“
$final_result=$result.Trim(','))
$smtpServer=“mail.server.com”
$msg=新对象Net.Mail.MailMessage
$smtp=新对象Net.Mail.SmtpClient($smtpServer)
$msg.From=”FromUser@email.com"
$msg.To.Add($final_result)
$msg.subject=“需要更新”
$msg.IsBodyHTML=$true
$body=@'
您好,请阅读此测试电子邮件。
'@ 
$msg.body=$body
}
$smtp.Send($msg)

很酷,谢谢。每个用户都收到了电子邮件。为了只发送一封包含所有用户的电子邮件,我需要做什么?相同。将发送选项保留在循环之外。在将所有收件人添加到“收件人”列表中后,它只会发送一次。@Bondo:如果答案对您有帮助,请接受,这将是非常有用的。看起来,当我将$smtp.send($msg)移出循环时,它只会将电子邮件发送到列出的最后一个电子邮件地址。不,您必须稍微更改逻辑。您必须将所有收件人添加到第一部分“我的一个”。然后我们必须使用send。所以所有的接收者将被添加到一个变量中,然后我们可以触发。否则给我点时间。一旦我自由了,我会做到的。