user2959441
user2959441

Reputation: 37

Sending to multiple reciepients powershell

i use the script below to send emails from a powershell script.

$smtpServer = "mail.company.com"
$smtpFrom = "Check <[email protected]>"
$smtpTo = "[email protected]"
$messageSubject = "Daily Check from $thedate"

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true

$message.Body = $Body | ConvertTo-HTML -head $style -body $Body

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)

It works fine untill i add more reciepients like this...

$smtpTo = "[email protected]", "[email protected]"

I also tried putting it inside an array like this....

$smtpTo = @("[email protected]", "[email protected]")

None of them work for me. Hope someone can help

Upvotes: 0

Views: 1090

Answers (1)

JNK
JNK

Reputation: 65147

The .To property of System.Mail.MailMessage is a collection that you can add emails to.

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$Message.to.add('[email protected]')
$message.Subject = $messageSubject
$message.IsBodyHTML = $true

Upvotes: 3

Related Questions