Using Send-MailMessage with multiple recipients

Welcome Forums General PowerShell Q&A Using Send-MailMessage with multiple recipients

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #131268

    Participant
    Points: 73
    Rank: Member

    I have a script that sends out an email to a large number of distribution lists as a final steps. In testing, if I send it to one person it works fine. The issue comes in when I am trying to send to multiple recipients. I have looked at the Send-MailMessage help topic and it expects a string[] for the -To field. I would think that I could do something like this:

    $sTo = "group1@mycompany.com","group2@mycompany.com","group3@mycompany.com"

    or:

    $sTo = "group1@mycompany.com;group2@mycompany.com;group3@mycompany.com"

    or even:

    [string[]]$sTo = "group1@mycompany.com","group2@mycompany.com","group3@mycompany.com"

    But all of them generate the same error:

    Cannot process argument transformation on parameter 'msgrecipient'. Cannot convert value to type System.String.

    I have tried putting the names into an array and then using .Split or .ToString, but still cannot seem to get the syntax correct. Any suggestions would be welcome.

  • #131270

    Participant
    Points: 639
    Helping Hand
    Rank: Major Contributor

    String[] is a string array, which is your first example:

    $sTo = "group1@mycompany.com","group2@mycompany.com","group3@mycompany.com"
    

    The following works fine for me (Office 365 SMTP):

    $cred = get-credential
    
    $params = @{
        To = 'rob.simmers@email1.com', 'rs@email2.com'
        From = 'rob.simmers@email1.com' 
        Subject = 'Test Email'
        Body = 'Testing for Powershell.org'
        BodyAsHtml = $true
        SmtpServer = 'smtp.office365.com' 
        UseSsl = $true
        Credential = $cred 
        Port = 587
    }
    
    Send-MailMessage @params
    
  • #131552

    Participant
    Points: 73
    Rank: Member

    Thanks, this seems to work in testing. I still am unsure why what I was trying wouldn't work.

The topic ‘Using Send-MailMessage with multiple recipients’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort