![](/img/trans.png)
[英]PowerShell Send-MailMessage to multiple recipients from scheduled task
[英]Powershell send-mailmessage - email to multiple recipients
我有這個 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
$recipients = "Marcel <marcel@turie.eu>, Marcelt <marcel@nbs.sk>"
是的類型string
需要傳遞給send-mailmessage
一個string[]
類型(數組):
[string[]]$recipients = "Marcel <marcel@turie.eu>", "Marcelt <marcel@nbs.sk>"
我認為沒有強制轉換為string []為powershell的強制規則做了工作:
$recipients = "Marcel <marcel@turie.eu>", "Marcelt <marcel@nbs.sk>"
是object[]
類型,但可以做同樣的工作。
只需創建一個Powershell數組即可
$recipients = @("Marcel <marcel@turie.eu>", "Marcelt <marcel@nbs.sk>")
相同的方法可用於附件
$attachments = @("$PSScriptRoot\image003.png", "$PSScriptRoot\image004.jpg")
您必須先將字符串轉換為字符串數組 ,如下所示:
$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
要定義一個字符串數組,使用$ 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
沒錯,每個地址都需要引用。 如果命令行中列出了多個地址,則如果同時指定了人員友好和電子郵件地址部分,則Send-MailMessage會喜歡它。
這是一個完整的(gmail)和簡單的解決方案...只需使用正常; 分隔符...最好以傳遞方式傳入。
$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
為了獲得最佳行為,請使用這樣的方法創建一個類
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
不需要所有復雜的鑄造或拆分成一個數組。 我只是通過更改語法來解決這個問題:
$recipients = "user1@foo.com, user2@foo.com"
對此:
$recipients = "user1@foo.com", "user2@foo.com"
我在Azure VM上使用Powershell和SendGrid向更多收件人發送郵件的完整解決方案:
# Set your API Key here
$sendGridApiKey = '......your Sendgrid API key'
# (only API permission to send mail is sufficient and safe')
$SendGridEmail = @{
# Use your verified sender address.
From = 'my@email.com'
# Specify the email recipients in an array.
To = @('person01@domainA.com','person02@domainB.com')
Subject = 'This is a test message via SendGrid'
Body = 'This is a test message from Azure send by Powershell script on VM using SendGrid in Azure'
# DO NO CHANGE ANYTHING BELOW THIS LINE
SmtpServer = 'smtp.sendgrid.net'
Port = 587
UseSSL = $true
Credential = New-Object PSCredential 'apikey', (ConvertTo-SecureString $sendGridApiKey -AsPlainText -Force)
}
# Send the email
Send-MailMessage @SendGridEmail
在 Azure 中的一台 VM 上使用 Powershell 5.1 進行了測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.