我有这个powershell脚本来发送附件的电子邮件,但是当我添加多个收件人时,只有第一个收到邮件。我已经阅读了文档,仍然无法弄明白。谢谢
$recipients = "Marcel <marcel@turie.eu>, Marcelt <marcel@nbs.sk>"
Get-ChildItem "C:\Decrypted\" | Where {-NOT $_.PSIsContainer} | foreach {$_.fullname} |
send-mailmessage -from "primasfrb@nbs.sk" `
-to "$recipients" `
-subject "New files" `
-body "$teloadmin" `
-BodyAsHtml `
-priority High `
-dno onSuccess, onFailure `
-smtpServer 192.168.170.61
答案 0 :(得分:77)
$recipients = "Marcel <marcel@turie.eu>, Marcelt <marcel@nbs.sk>"
是string
的类型,您需要传递给send-mailmessage
一个string[]
类型(数组):
[string[]]$recipients = "Marcel <marcel@turie.eu>", "Marcelt <marcel@nbs.sk>"
我认为不是强制转换为字符串[]为powershell的强制规则做了工作:
$recipients = "Marcel <marcel@turie.eu>", "Marcelt <marcel@nbs.sk>"
是object[]
类型,但可以执行相同的工作。
答案 1 :(得分:23)
只需创建一个Powershell数组就可以了解
$recipients = @("Marcel <marcel@turie.eu>", "Marcelt <marcel@nbs.sk>")
相同的方法可用于附件
$attachments = @("$PSScriptRoot\image003.png", "$PSScriptRoot\image004.jpg")
答案 2 :(得分:5)
您必须先将字符串转换为字符串数组,如下所示:
$recipients = "Marcel <marcel@turie.eu>,Marcelt <marcel@nbs.sk>"
[string[]]$To = $recipients.Split(',')
然后像这样使用Send-MailMessage
:
Send-MailMessage -From "primasfrb@nbs.sk" -To $To -subject "New files" -body "$teloadmin" -BodyAsHtml -priority High -dno onSuccess, onFailure -smtpServer 192.168.170.61
答案 3 :(得分:4)
要定义字符串数组,使用起来会更舒服 $ var = @('User1','User2')。
$servername = hostname
$smtpserver = 'localhost'
$emailTo = @('username1 <user1@dom.com>', 'username2<user2@dom.com>')
$emailFrom = 'SomeServer <user3@dom.com>'
Send-MailMessage -To $emailTo -Subject 'Low available memory' -Body 'Warning' -SmtpServer $smtpserver -From $emailFrom
答案 4 :(得分:2)
没错,每个地址都需要引用。如果您在命令行中列出了多个地址,则如果您同时指定人员友好和电子邮件地址部分,则Send-MailMessage会喜欢它。
答案 5 :(得分:0)
为了获得最佳行为,请使用类似
的方法创建一个类 using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddCommand("Send-MailMessage")
.AddParameter("SMTPServer", "smtp.xxx.com")
.AddParameter("From", "xxx@xxx.com")
.AddParameter("Subject", "xxx Notification")
.AddParameter("Body", body_msg)
.AddParameter("BodyAsHtml")
.AddParameter("To", recipients);
// invoke execution on the pipeline (ignore output) --> nothing will be displayed
PowerShellInstance.Invoke();
}
在这样的函数中调用这些实例:
public void sendEMailPowerShell(string body_msg, string[] recipients)
永远不要忘记为接收者使用字符串数组,如下所示:
string[] reportRecipient = {
"xxx <xxx.Fercher@xxx.com>",
"xxx <xxx@xxx.com>"
};
此消息可以作为方法本身的参数被覆盖,启用HTML编码!!
永远不要忘记在多个收件人的情况下使用字符串数组,否则只会使用字符串中的最后一个地址!!!
mail reportMail = new mail(); //instantiate from class
reportMail.sendEMailPowerShell(reportMessage, reportRecipient); //msg + email addresses
答案 6 :(得分:0)
这是一个完整的(gmail)和简单的解决方案...只需使用normal即可;定界符..最适合作为参数传递。
$to = "user1@domain.org;user2@domain.org"
$user = "italerts@domain.org"
$pass = ConvertTo-SecureString -String "pass" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential $user, $pass
$mailParam = @{
To = $to.Split(';')
From = "IT Alerts <italerts@domain.org>"
Subject = "test"
Body = "test"
SmtpServer = "smtp.gmail.com"
Port = 587
Credential = $cred
}
Send-MailMessage @mailParam -UseSsl