none
Unable to send email to multiple recipients! RRS feed

  • Question


  • Hi

    I have created a function to send an email as below and I'm trying to send the email to multiple recipients. But getting the below error:

    Send-MailMessage : The specified string is not in the form required for an e-mail address.
    At C:\scripts\MyLib.ps1:110 char:10
    +         Send-MailMessage <<<<  -To $toAddress -From $fromAddress  -Subject $subject -Body $body -SmtpServer $smtpServer -BodyAsHtml
        + CategoryInfo          : InvalidType: (:) [Send-MailMessage], FormatException
        + FullyQualifiedErrorId : FormatException,Microsoft.PowerShell.Commands.SendMailMessage

    Send-MailMessage : A recipient must be specified.
    At C:\scripts\MyLib.ps1:111 char:10
    +         Send-MailMessage <<<<  -To $toAddress -From $fromAddress  -Subject $subject -Body $body -SmtpServer $smtpServer -BodyAsHtml
        + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], InvalidOperationException
        + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.SendMailMessage


    Here is the function I'm using:

    # Add function to send e-mail

    [string] $smtpServer = "mail.abc.com"
    [string] $fromAddress="dba@abc.com"
    [string] $toAddress="user1@abc.com", "user2@abc.com"

    Function Send-Email([String] $smtpServer, [String] $toAddress, [String] $fromAddress, [String] $subject, [String] $body)
    {
            Send-MailMessage -To $toAddress -From $fromAddress  -Subject $subject -Body $body -SmtpServer $smtpServer -BodyAsHtml   
        }


    I'm able to send email to single user successfully with the above function but NOT multiple recipients

    Thanks

    Monday, April 11, 2011 6:05 PM

Answers

  • [string] $smtpServer = "mail.abc.com"
    [string] $fromAddress="dba@abc.com"
    [string[]] $toAddress="user1@abc.com", "user2@abc.com"
    
    Function Send-Email([String] $smtpServer, [String[]] $toAddress, [String] $fromAddress, [String] $subject, [String] $body)
    {
            Send-MailMessage -To $toAddress -From $fromAddress  -Subject $subject -Body $body -SmtpServer $smtpServer -BodyAsHtml    
        }
    PS > get-help Send-MailMessage -Parameter to
    
    -To <string[]>
    
    Your problem is :
    PS > $toAddress
    user1@abc.com user2@abc.com
    PS > $toAddress.gettype().fullname
    System.String
    
    Change to:
    PS > [string[]] $toAddress="user1@abc.com", "user2@abc.com"
    PS > $toAddress
    user1@abc.com
    user2@abc.com
    PS > $toAddress.gettype().fullname
    System.String[]
    • Proposed as answer by Just Karl Monday, April 11, 2011 7:32 PM
    • Marked as answer by Shay LeviModerator Tuesday, April 12, 2011 9:27 AM
    Monday, April 11, 2011 6:38 PM

All replies

  • [string] $smtpServer = "mail.abc.com"
    [string] $fromAddress="dba@abc.com"
    [string[]] $toAddress="user1@abc.com", "user2@abc.com"
    
    Function Send-Email([String] $smtpServer, [String[]] $toAddress, [String] $fromAddress, [String] $subject, [String] $body)
    {
            Send-MailMessage -To $toAddress -From $fromAddress  -Subject $subject -Body $body -SmtpServer $smtpServer -BodyAsHtml    
        }
    PS > get-help Send-MailMessage -Parameter to
    
    -To <string[]>
    
    Your problem is :
    PS > $toAddress
    user1@abc.com user2@abc.com
    PS > $toAddress.gettype().fullname
    System.String
    
    Change to:
    PS > [string[]] $toAddress="user1@abc.com", "user2@abc.com"
    PS > $toAddress
    user1@abc.com
    user2@abc.com
    PS > $toAddress.gettype().fullname
    System.String[]
    • Proposed as answer by Just Karl Monday, April 11, 2011 7:32 PM
    • Marked as answer by Shay LeviModerator Tuesday, April 12, 2011 9:27 AM
    Monday, April 11, 2011 6:38 PM
  • Thank you so much..It's worked!!
    Monday, April 11, 2011 11:29 PM
  • Or just remove the [string] cast such as : $toAddress="user1@abc.com", "user2@abc.com"

    That will automatically create an array of string

     

    • Proposed as answer by FunThomaseek Tuesday, September 6, 2016 11:34 AM
    Tuesday, April 12, 2011 2:47 AM
  • Hi,

    In your answer you've hard coded the distribution list.

    But what if you're extracting recipient list from xml file or from database, the above solution isn't working for me.

    I have saved the recipient list in xml file, when I read the data and assign it to array. I get the error message invalid character '""

    <Config>

      <To>"xyz@domain.com","abc@domain.com"</To>

    </Config>

    $MailTo = @($($Config[1]))

    Send-MailMessage -From $From -To $MailTo -Subject $Subject -SmtpServer $SMTPServer -Body $Body 

    Note: I even tried String[[]] $Mailto = $($Config[1]), it's not working too

    


    VJ

    Friday, August 18, 2017 6:46 AM
  • Hi, Below link of my blog may help you if you are trying to send email from PowerShell to multiple recipient which are in Variable or in file.

    http://nagpurchasysadmin.blogspot.in/2017/10/sending-mail-to-multiple-recipient-at.html

    Please mark it as answer if you it helps
    Wednesday, October 18, 2017 2:00 PM