Hi All,
Have you ever tried sending mail to multiple
recipient at once using PowerShell with Send-MailMessage ?
If yes, then you might also have faced issues while trying this. The most probable error you would have face can be the below one.
Error: An invalid character was found in the mail header: ','
How to send email multiple recipient at once using PowerShell.
Let’s get started :
Issue /Problem
Solution:
If yes, then you might also have faced issues while trying this. The most probable error you would have face can be the below one.
Error: An invalid character was found in the mail header: ','
Or there are few other errors which may generate while
trying this. We can ignore the other errors and will not require me to put the
list of all those alert. Because my main goal behind writing this post is to
show you…
How to send email multiple recipient at once using PowerShell.
How to solve Error: An
invalid character was found in the mail header: ','
Issue /Problem
$To = 'recipient1@xyz.com,recipient1@xyz.com' send-MailMessage -From sender@xyz.com -to $To -Subject "Test Subject" -Body "Test Body" -SmtpServer 127.0.0.1 -port 25If you try to send emails to multiple recipients with above code, you will get below error message.
Solution:
$To = 'recipient1@xyz.com,recipient1@xyz.com' send-MailMessage -From sender@xyz.com -to ($To -split ',' ) -Subject "Test Subject" -Body "Test Body" -SmtpServer 127.0.0.1 -port 25
This will resolve your issue!!!
Thank You!!
The $To variable can also be filled from recipient list
present in file in below way to get successful outcome.
$To = Get-Content "C:\test\users.txt"; send-MailMessage -From sender@xyz.com -to ($To -split ',' ) -Subject "Test Subject" -Body "Test Body" -SmtpServer 127.0.0.1 -port 25
The file should look like below.
Important observations.
The discussed issue mainly arises when we are trying to call
recipient list from variable/ or from file through variable.
Kindly Share your experience in comment
section with my blog/post if it did help you !!
Stay
Blessed!! Thank You!!
Hello! Thanks for this post. Here is my code that I CAN NOT get to send an email to multiple recipients:
ReplyDeleteparam (
[Parameter(Mandatory)]
[ValidateScript({Test-Connection -ComputerName $_ -Quiet -Count 1})]
[string[]]$Computername,
[string]$LogFilePath = 'D:\ServerAdmins\Audit\Audit.csv',
[string]$SmtpServer = 'webmail.test.org',
[string]$EmailFrom = 'user1@test.org',
[string]$EmailSubject = 'New Local Administrator Detected',
[string]$EmailRecipient = 'user1@test.org,user2@test.org'
What am I doing wrong?
Hey, sorry for the late response. in case you are still looking for the reason.
DeleteJust look at my -to section in above script, you will find that i am splitting the email id on character "," . that is what you need to do.
Hey, sorry for the late response. in case you are still looking for the reason.
ReplyDeleteJust look at my -to section in above script, you will find that i am splitting the email id on character "," . that is what you need to do.
Amazing tip, man!
ReplyDeleteThank you!
Recently had the same issue
Dude... This is awesome. This was exactly what I was looking for.. you solved the issue that I was breaking my head for 4 days. :) :)
ReplyDeleteThank you so much..!!