【问题标题】:How to add multiple recipients to an xml configuration file and send a mail to those recipients from a PowerShell script如何将多个收件人添加到 xml 配置文件并从 PowerShell 脚本向这些收件人发送邮件
【发布时间】:2021-06-28 19:27:18
【问题描述】:

我正在尝试检索 URL 的内容,根据某些条件对其进行过滤,并与多个邮件收件人共享此过滤结果。我的邮件收件人和 smtp 设置存在于 xml 文件中。我从 power shell 脚本中调用该 xml 文件并发送电子邮件。当 xml 文件只有 1 个收件人时,代码工作得非常好。 `

[CmdletBinding()]
param ()
#-------------------------------------------------
#  Variables
#-------------------------------------------------
Start-Transcript
$myDir = "C:\Users\abc\Desktop"
$request='https://reqres.in/api/users?per_page=25'
invoke-restmethod -Uri $request | ConvertTo-Json -Depth 100  | Out-file C:\Users\abc\Desktop\API_Content.json
$variable=invoke-webrequest $request | ConvertFrom-Json # WE CAN ALSO USE $variable =Get-Content “C:\Users\abc\Desktop\API_Content.json”#
$data=$variable.data | Where-Object {$_.id -ge 7 -and $_.id -le 9}
#$data | Format-List *


# Import email settings from config file

[xml]$ConfigFile = Get-Content "$myDir\Settings.xml"


# Create email content
$smtpsettings = @{

    to = $ConfigFile.Settings.EmailSettings.MailTo

    from = $ConfigFile.Settings.EmailSettings.MailFrom

    subject = "TEST EMAIL"

    smtpServer = $ConfigFile.Settings.EmailSettings.SMTPServer

    }

 

[String]$messagebody = ""

#-------------------------------------------------
#  Script
#-------------------------------------------------

try
{
 #Send-MailMessage @smtpsettings -Body $body  -ErrorAction STOP
 $smtp = New-Object Net.Mail.SmtpClient($smtpsettings.SmtpServer)
 $smtp.Send($smtpsettings.To,$smtpsettings.From, $smtpsettings.Subject, $messagebody)
}

catch
{
    Write-Warning $_.Exception.Message
}

#-------------------------------------------------
#  The End
#------------------------------------------------- 

`当我向 xml 文件添加多个收件人而不是一个收件人时,我收到错误消息:" 警告:使用“4”参数调用“发送”的异常:“在邮件标题中发现无效字符:','。”

XML FILE SETTINGS (IGNORE THE SMPTP SERVER NAME)

我需要将消息正文发送给 4 个收件人,而不是 1 个收件人,保持上述代码和方法不变。

请求任何帮助。

【问题讨论】:

  • $ConfigFile.Settings.EmailSettings.MailTo的值是多少?
  • abc@xyz.com25abc@xyz .com'you@xyz.com,me@xyz.com'
  • @lit - 你能回复吗?
  • 请看@Cpt.Whale的回答。

标签: powershell email


【解决方案1】:

您使用的 Send() 方法仅接受单个字符串作为收件人电子邮件地址,但它可以是逗号分隔的列表,只要它仍然用引号括起来。

$smtpsettings = @{
    to = 'me@domain.com,you@domain.com' # This will work (a single string)
}
$smtpsettings = @{
    to = 'me@domain.com','you@domain.com' # This (a string array) only sends to the last address for some reason
}

但是要得到您看到的错误,您的列表中可能有一个无效的电子邮件地址或额外的逗号:

$smtpsettings = @{
    to = 'me@domain.com,you@domain.com,' # This will error (a trailing comma)
}

'Exception calling "Send" with "4" argument(s): "An invalid character was found in the mail header: ','."'

注意:从 powershell v2 开始,您应该改用Send-MailMessage。它几乎可以接受任何地址,并且可以使用 SSL、smtp 凭据和其他一些功能而无需担心:

Send-MailMessage -From 'me@domain.com' -To @('you@domain.com','me@domain.com') -Body 'Hello World' -SmtpServer 'Mail.domain.com' 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-15
    • 2012-05-01
    • 1970-01-01
    • 2017-09-26
    • 2012-05-18
    • 2019-03-01
    相关资源
    最近更新 更多