13

Oki, so im working on outlook .msg templates. Opening them programmatically, inserting values base on what's in my db.

ex. when i want to add multiple reciepients at "To" field, instead of doing as following,

   mailitem.To = a + ";" + b + ";" + c;

i do whats below, which is simpler, especially when i'm doing it in a loop.

   mailitem.Recipients.add("a");
   mailitem.Recipients.add("b");
   mailitem.Recipients.add("c");

My problem is, i also want to add multiple recipients at "CC" field and the function above only works for "To" field. How can i add multiple recipients to "CC" field without having to do string manipulation.

normally i would add recipients to cc like so,

   mailitem.CC = a + ";" + b + ";" + c;

im using interop.outlook and creating an mailitem from template.

Thanks in advance.

Mana
  • 1,925
  • 6
  • 39
  • 55

2 Answers2

14

Suppose If you have two List of recipients, then you can do like this.

Edit: Included full code.

var oApp = new Microsoft.Office.Interop.Outlook.Application();
var oMsg = (MailItem) oApp.CreateItem(OlItemType.olMailItem);

Recipients oRecips = oMsg.Recipients;
List<string> sTORecipsList = new List<string>();
List<string> sCCRecipsList = new List<string>();

sTORecipsList.Add("ToRecipient1");

sCCRecipsList.Add("CCRecipient1");
sCCRecipsList.Add("CCRecipient2");
sCCRecipsList.Add("CCRecipient3");

Recipients oRecips = oMsg.Recipients;

foreach (string t in sTORecipsList)
{
    Recipient oTORecip = oRecips.Add(t);
    oTORecip.Type = (int) OlMailRecipientType.olTo;
    oTORecip.Resolve();
}

foreach (string t in sCCRecipsList)
{
    Recipient oCCRecip = oRecips.Add(t);
    oCCRecip.Type = (int) OlMailRecipientType.olCC;
    oCCRecip.Resolve();
}

oMsg.HTMLBody = "Test Body";
oMsg.Subject = "Test Subject";
oMsg.Send();
Ramesh Durai
  • 2,666
  • 9
  • 32
  • 55
  • can't get it to work, are you sure it should work with outlook 2007 / office 2007 ? – Mana May 22 '13 at 13:15
  • @Mana: What was the problem you're facing? – Ramesh Durai May 22 '13 at 13:16
  • could you explain this line? Recipient oCCRecip = oRecips.Add(p_sUserID); where or what is p_sUserID doing in .add() ? – Mana May 22 '13 at 13:23
  • @Mana: Sorry. Forgot to edit that code part. Check the edited code – Ramesh Durai May 22 '13 at 13:27
  • thanks, trying it out now, just one thing, how do you reference Microsoft.Office.Interop.Outlook.Application ? i mean where do you find it? because im doing as following //Outlook.Application oApp = new Outlook.Application(); – Mana May 22 '13 at 13:30
  • Microsoft.Office.Interop.Outlook.Application and Outlook.Application was same if you are using the statement `using Microsoft.Office.Interop.Outlook;`. Anyway to add,Right click References --> Add Reference --> COM tab --> Microsoft Outlook 12.0 Object Library – Ramesh Durai May 22 '13 at 13:36
  • hi again, im having problems with this line: oCCRecip.Type = (int) OlMailRecipientType.olCC; cant find Type oCCRecip.Type – Mana May 22 '13 at 13:53
  • Do you have this line? `Recipient oCCRecip = oRecips.Add(t);` – Ramesh Durai May 22 '13 at 13:57
3

Use the Recipients property as documented here (look for the second example). you can add a lot of people to the collection and then change the destination type from to to CC.

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
  • that example is for vba, im programming in c#, and even if i were programming in vba, the CreateItem would not work since im using office2007 – Mana May 22 '13 at 12:58
  • I thought they had the same API. But you know what, I like Ramesh's answer better than mine. – Geeky Guy May 22 '13 at 13:00