locked
Send-MailMessage is not working when email addresses are passed as parameters RRS feed

  • Question

  • Hi, I have below code in a power shell script and i am calling the script with parameters like below, but it is not sending email. 

    .\mail.ps1 -MailFrom "andand@cmd.com" -MailToList "andand@cmd.com, prasad@cmd.com"

    ---------------------------------------------------My code-------------------------------------------------------------------------

    param(
    [string]$MailFrom,
    [string]$MailToList
    )

    Send-MailMessage -To $MailToList -From $MailFrom  -Subject "Test Reults - for ADP Module" -SmtpServer smtp-xz.cmd.com
    ----------------------------------------------------------------------------------------------------------------------------

    if i replace variables with values in the code it is working fine. i am not able to make it work by passing values as parameters. not sure what mistake i am doing here, please help.

    Sunday, September 29, 2019 12:05 PM

All replies

  • Please format you code as code: How to Use the Code Feature in a TechNet Forum Post.

    You specified your $MailToList parameter as single string. It should be a multiple string to fit to the -To parameter of Send-MailMessage. So you should change [string]$MailToList to [string[]]$MailToList and provide your email addresses as comma separated list - not as a single string ... -MailToList "andand@cmd.com", "prasad@cmd.com"


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Sunday, September 29, 2019 2:08 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Thursday, October 3, 2019 1:51 AM