Unable to send email to multiple recipients - Poweshell

  • 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:110 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 to multiple recipients

    Thanks

  • i believe the toAddress requires a semicolon delimited list of recipients, not a comma delimited one., so you'd need this for the variablbe, right?

    [string] $toAddress="user1@abc.com;user2@abc.com"

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I already tried [string] $toAddress="user1@abc.com;user2@abc.com". But getting the below error: (In Powershell, I know that, we should use comma but do not know how to use it)

    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:110 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

    From Powershell Help, It should be comma and I'm giving the same as the example says. But it's not working for metxtPost_CommentEmoticon(':hehe:');

    C:\PS>send-mailmessage -from "User01 <user01@example.com>" -to "User02 <user02@example.com>", "User03 <user03@example.com>" -subject "Sending the Attachment" -body "Forgot

    to send the attachment. Sending now." -Attachment "data.csv" -priority High -dno onSuccess, onFailure -smtpServer smtp.fabrikam.com

    Description

    -----------

    This command sends an e-mail message with an attachment from User01 to two other users.

  • enclosing the email address within brackets worked for us...

    $strTo = ("email.1@abc.com","email.2@abc.com")

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply