powershell - Powershell将电子邮件发送给Excel电子表格中的收件人

标签 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($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)

最佳答案

因此,您必须在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)

}

希望能帮助到你。

关于powershell - Powershell将电子邮件发送给Excel电子表格中的收件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42800124/

相关文章:

powershell - 按名称将管道输入发送到不匹配的属性名称

html - 电子邮件背景图像用作 URL 但不用作目录

java - 希望创建一次性结合 Facebook 和电子邮件帐户的桌面应用程序 - 陷入第一个障碍?

ruby-on-rails - 如何生成取消订阅电子邮件的链接

shell - 使用 powershell {send-mailmessage} 检查 SMTP 服务器是否正常工作

.net - Powershell 从字符串中生成 System.Type

powershell - 创建目录结构(如果Powershell中不存在)

c# - 使用 ProjectItems.item

azure - 将 PowerShell 对象数据记录到 Azure blob,因为 CSV 缺少换行符

php 邮件附件