3

I currently have existing code that automates and email and sends files. I now need to add a cc. I have looked all over, but can't seem to find out with my existing code. Any help would be greatly appreciated. Thank you.

         private void button13_Click(object sender, EventArgs e)
    {
        //Send Routing and Drawing to Dan
        // Create the Outlook application by using inline initialization. 
        Outlook.Application oApp = new Outlook.Application();
        //Create the new message by using the simplest approach. 
        Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
        //Add a recipient
        Outlook.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add("email@email.com");
        oRecip.Resolve();
        //Set the basic properties. 
        oMsg.Subject = "Job # " + textBox9.Text + " Release (" + textBox1.Text + ")";
        oMsg.HTMLBody = "<html><body>";
        oMsg.HTMLBody += "Job # " + textBox9.Text + " is ready for release attached is the Print and Routing (" + textBox1.Text + ")";
        oMsg.HTMLBody += "<p><a href='C:\\Users\\RussellS\\Desktop\\Russell Eng Reference\\" + textBox1.Text + ".PDF'>" + textBox1.Text + " Drawing";
        oMsg.HTMLBody += "<p><a href='C:\\Users\\RussellS\\Desktop\\" + textBox1.Text + ".PDF'>" + textBox1.Text + " Routing" + "</a></p></body></html>";
        //Send the message
        oMsg.Send();
        //Explicitly release objects. 
        oRecip = null;
        oMsg = null;
        oApp = null;
        MessageBox.Show(textBox1.Text + " Print and Routing Sent");
    }
Mat
  • 202,337
  • 40
  • 393
  • 406
Russell Saari
  • 985
  • 11
  • 26
  • 41

2 Answers2

4

According to MSDN there's a CC property on the MailItem class.

string CC { get; set; }

Which can be used to set the names of the CC recipients.

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._mailitem.cc.aspx

To modify the recipients you can add them to the Recipients collection:

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.recipients.aspx

Which you would use like:

oMsg.Recipients.Add("foo@bar.com");
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
  • that works actually, but it adds to the TO: on outlook and not the cc so would I somehow have to string CC in the line somewhere? – Russell Saari Sep 12 '11 at 15:55
  • As far as I'm aware, setting the CC property with the names of the CC recipients to something matching a recipient in the Recipients collection will be enough. – Jamie Dixon Sep 12 '11 at 16:02
  • 7
    I know this answer was accepted, I just wanted to add that Microsoft recommends you manipulate the recipients through the Recipients Collection. The To, CC and BCC properties should be used only to read the recipients. To set a CC recipient you would set the Type property to olCC (or olBCC for BCC recipients) right after Jamie's code above that adds a new recipient. – JimmyPena Sep 13 '11 at 21:15
1

Please follow this code for adding CC and BCC:

private void SetRecipientTypeForMail()
{
    Outlook.MailItem mail = Application.CreateItem(
        Outlook.OlItemType.olMailItem) as Outlook.MailItem;
    mail.Subject = "Sample Message";
    Outlook.Recipient recipTo =
        mail.Recipients.Add("someone@example.com");
    recipTo.Type = (int)Outlook.OlMailRecipientType.olTo;
    Outlook.Recipient recipCc =
        mail.Recipients.Add("someonecc@example.com");
    recipCc.Type = (int)Outlook.OlMailRecipientType.olCC;
    Outlook.Recipient recipBcc =
        mail.Recipients.Add("someonebcc@example.com");
    recipBcc.Type = (int)Outlook.OlMailRecipientType.olBCC;
    mail.Recipients.ResolveAll();
    mail.Display(false);
}