0

I have a code written to generate a report file in HTML format using a powershell.

Used the below code to send an email with that HTML as an attachment. But unable to cc the user to the mail.

Code:

$subject = "Subject Line" 
$body = Get-Content "D:\temp\ABC\TEST\report.htm" 
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost 
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body 
$msg.isBodyhtml = $true 
$smtp.send($msg) 

Could someone please suggest me on how to send cc using the above syntax?

I can use the Send-MailMessage to accommodate the cc list.But in that case i wouldn't be able to send the HTML output as a body message.

1
  • could someone please help me in getting the bottom of the issue? Jul 19, 2016 at 21:43

2 Answers 2

1

MailMessage has a property called CC that you can set like you set the To line. $msg.CC = "[email protected]". Send-MailMessage has a -BodyAsHtml switch. Pick your poison.

3
  • Thanks for your reply.I've already tried this $msg.cc.Ad(...) which didnt work.Though the powershell completes without any error,email doesnt get delivered to the cc list.Could you please hlp me in getting the -bodyAsHtml switch for the Send-MailMessage or may the syntax for adding $msg.cc ? Jul 18, 2016 at 16:41
  • I used $msg.CC.Add("[email protected]") and then ran $msg and the output showed the CC line populated - does that happen to you when you look at yours and you're adding it before you're sending it, right? I know that's a silly question...
    – Chris N
    Jul 18, 2016 at 16:46
  • Yes that does print..but the actual email is not sent to the cc listed member.May i request you to help me with the option you have suggested for send-MailMessage to have the bodyAsHtml flag set to true? Jul 19, 2016 at 8:19
1

you should read the help file and is already have pretty nice examples.

Get-Help Send-MailMessage -Examples

If you are not getting any examples, do Update-Help

Send-MailMessage -To '[email protected]' -From '[email protected]' -Cc '[email protected]'  -body $bodyhtml -BodyAsHtml

Regards

kvprasoon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.