I have this powershell script to sending emails with attachments, but when I add multiple recipients, only the first one gets the message. I've read the documentation and still can't figure it out. Thank you
$recipients = "Marcel <marcel@turie.eu>, Marcelt <marcel@nbs.sk>"
Get-ChildItem "C:\Decrypted\" | Where {-NOT $_.PSIsContainer} | foreach {$_.fullname} |
send-mailmessage -from "primasfrb@nbs.sk" `
-to "$recipients" `
-subject "New files" `
-body "$teloadmin" `
-BodyAsHtml `
-priority High `
-dno onSuccess, onFailure `
-smtpServer 192.168.170.61
That's right, each address needs to be quoted. If you have multiple addresses listed on the command line, the Send-MailMessage likes it if you specify both the human friendly and the email address parts.
To define an array of strings it is more comfortable to use $var = @('User1 ', 'User2 ').
Just creating a Powershell array will do the trick
The same approach can be used for attachments
You must first convert the string to a string array, like this:
Then use
Send-MailMessage
like this:to send a .NET / C# powershell eMail use such a structure:
for best behaviour create a class with a method like this
Whereby these instance is called in a function like:
Never forget to use a string array for the recepients, which can be look like this:
body_msg
this message can be overgiven as parameter to the method itself, HTML coding enabled!!
recipients
never forget to use a string array in case of multiple recipients, otherwise only the last address in the string will be used!!!
calling the function can look like this:
ThumbUp
is type of
string
you need pass tosend-mailmessage
astring[]
type (an array):I think that not casting to string[] do the job for the coercing rules of powershell:
is
object[]
type but can do the same job.