sending email to multiple users using power shell.


hi experts,

not able send mail below code. 

$body = convertto-html -head $head -body "$reportoutput"
string[]]$recipient =("mail_id_1@email.com","mail_id_2@email.com")
$smtpserver = "smtp server name"
$msg = new-object net.mail.mailmessage
$smtp = new-object net.mail.smtpclient($smtpserver)
$msg.from = "from@email.com"
$msg.to.add($recipient)
$msg.subject = "email message"
$msg.isbodyhtml = 1
$msg.body = $body
$smtp.send($msg)

please find below error 

exception calling "add" "1" argument(s): "the specified string not in form required e-mail address."
@ z:\personal\powershell\script\remote.ps1:20 char:12
+ $msg.to.add <<<< ($recipient)
    + categoryinfo          : notspecified: (:) [], methodinvocationexception
    + fullyqualifiederrorid : dotnetmethodexception
 
exception calling "send" "1" argument(s): "a recipient must specified."
@ z:\personal\powershell\script\remote.ps1:24 char:11
+ $smtp.send <<<< ($msg)
    + categoryinfo          : notspecified: (:) [], methodinvocationexception
    + fullyqualifiederrorid : dotnetmethodexception

this how it

$recipient ='mail_id_1@email.com', 'mail_id_2@email.com'
 $smtpserver = "server name"
 foreach ($r in $recipient){
    $msg = new-object net.mail.mailmessage
    $smtp = new-object net.mail.smtpclient($smtpserver)
    $msg.from = "mail_id_1@email.com"
    $msg.to.add($r)
    $msg.subject = "email message"
    $msg.isbodyhtml = 1
    $msg.body = $body
    $smtp.send($msg)
 }

 


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

CRL Revocation always failed

Failed to query the results of bpa xpath

RDS 2012 r2 collections show black screen and then close