Search code examples
powershellsmtpsendmail

Adding a CC to a email being sent from powershell


I'm sending a report out via email in Powershell and I would like to add a CC, but everything I've tried thusfar isnt working.

heres my sendmail code...

 $ExportedReport = "$WorkFolder\InactiveUserReport.csv"
 $EmailTo = "[email protected]"
 $EmailFrom = "[email protected]"
 $Subject = "REPORT BLAH BLAH" 
 $Body = "blah blah blah."
 $SMTPServer = "mysmtpserver" 
 $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
 $attachment = New-Object System.Net.Mail.Attachment($ExportedReport)
 $SMTPMessage.Attachments.Add($attachment)
 $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) 
 $SMTPClient.EnableSsl = $false 
 $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("myusername", "mypassword"); 
 $SMTPClient.Send($SMTPMessage)

Any help on how I can modify this to add a CC would be wonderful!


Solution

  • Your MailMessage has a CC property.

    Use the following:

    $SMTPMessage.cc.Add("[email protected]")