Powershell send-mailmessage - email to multiple recipients

前端 未结 7 2058
小蘑菇
小蘑菇 2020-11-29 07:10

I have this powershell script to sending emails with attachments, but when I add multiple recipients, only the first one gets the message. I\'ve read the documentation and s

相关标签:
7条回答
  • 2020-11-29 07:31

    Just creating a Powershell array will do the trick

    $recipients = @("Marcel <marcel@turie.eu>", "Marcelt <marcel@nbs.sk>")
    

    The same approach can be used for attachments

    $attachments = @("$PSScriptRoot\image003.png", "$PSScriptRoot\image004.jpg")
    
    0 讨论(0)
  • 2020-11-29 07:36
    $recipients = "Marcel <marcel@turie.eu>, Marcelt <marcel@nbs.sk>"
    

    is type of string you need pass to send-mailmessage a string[] type (an array):

    [string[]]$recipients = "Marcel <marcel@turie.eu>", "Marcelt <marcel@nbs.sk>"
    

    I think that not casting to string[] do the job for the coercing rules of powershell:

    $recipients = "Marcel <marcel@turie.eu>", "Marcelt <marcel@nbs.sk>"
    

    is object[] type but can do the same job.

    0 讨论(0)
  • 2020-11-29 07:39

    You must first convert the string to a string array, like this:

    $recipients = "Marcel <marcel@turie.eu>,Marcelt <marcel@nbs.sk>"
    [string[]]$To = $recipients.Split(',')
    

    Then use Send-MailMessage like this:

    Send-MailMessage -From "primasfrb@nbs.sk" -To $To -subject "New files" -body "$teloadmin" -BodyAsHtml -priority High -dno onSuccess, onFailure -smtpServer 192.168.170.61
    
    0 讨论(0)
  • 2020-11-29 07:40

    to send a .NET / C# powershell eMail use such a structure:

    for best behaviour create a class with a method like this

     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();
                }              
    

    Whereby these instance is called in a function like:

            public void sendEMailPowerShell(string body_msg, string[] recipients)
    

    Never forget to use a string array for the recepients, which can be look like this:

    string[] reportRecipient = { 
                            "xxx <xxx.Fercher@xxx.com>",
                            "xxx <xxx@xxx.com>"
                            };
    

    body_msg

    this message can be overgiven as parameter to the method itself, HTML coding enabled!!

    recipients

    never forget to use a string array in case of multiple recipients, otherwise only the last address in the string will be used!!!

    calling the function can look like this:

            mail reportMail = new mail(); //instantiate from class
            reportMail.sendEMailPowerShell(reportMessage, reportRecipient); //msg + email addresses
    

    ThumbUp

    0 讨论(0)
  • 2020-11-29 07:42

    To define an array of strings it is more comfortable to use $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
    
    0 讨论(0)
  • 2020-11-29 07:42

    That's right, each address needs to be quoted. If you have multiple addresses listed on the command line, the Send-MailMessage likes it if you specify both the human friendly and the email address parts.

    0 讨论(0)
提交回复
热议问题