0

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

$smtpServer = "mail.company.com"
$smtpFrom = "Check <check@company.com>"
$smtpTo = "user1@company.com"
$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 = "user1@company.com", "user2@company.com"

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

$smtpTo = @("user1@company.com", "user2@company.com")

None of them work for me. Hope someone can help

1 Answers1

3

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('myotheremail@mydomain.net')
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
JNK
  • 63,321
  • 15
  • 122
  • 138