共用方式為


Updating Recipients using Outlook Object Model

One of my customers was migrating his Exchange Client Extension code to an Outlook add-in for Outlook 2010 and wanted to modify the recipient using Outlook Object Model(OOM) and ran into issues. What is the problem? In Exchange Client Extension you could hook into events like IExchExtMessageEvents::OnCheckNames and IExchExtMessageEvents::OnChecknameComplete and then from this event you would get an IExchExtCallBack interface. Using the IExchExtCallBack interface you could use the functions GetRecipients and SetRecipients to modify the recipient collection.

The BIG question is, how can this be done using OOM?

Unfortunately there is nothing in the Outlook Object Model that allows you to modify a recipient. We do have a workaround of first reading the existing recipients, removing the existing recipients, building a new recipient list with the changes and then adding it back to the message.

Below is some sample C# code that demonstrated this. In this sample I first delete all the recipients from the message, build a fresh list and then add them back to the message and finally a tweak to get the UI to update.

 private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.Inspectors.NewInspector += 
        new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
}

private Outlook::MailItem mailItem = null;

void Inspectors_NewInspector(Outlook.Inspector Inspector)
{
    mailItem = Inspector.CurrentItem as Outlook::MailItem;
    if (mailItem != null) {
        mailItem.BeforeCheckNames += new Outlook.ItemEvents_10_BeforeCheckNamesEventHandler(mailItem_BeforeCheckNames);
    }
}

void mailItem_BeforeCheckNames(ref bool Cancel)
{
    // In order to be able to add recipients, set their addressEntries, and have them display, we need to
    // 1) Remove ALL recipients from the message
    // 2) Re-add them, and set their address entries
    // 3) To get the UI to update, add and remove a final recipient
    
    // Step 1: remove all recipients
    int Count = mailItem.Recipients.Count;
    while (Count > 0) {
        mailItem.Recipients.Remove(1);
        Count = mailItem.Recipients.Count;
    }
   
    // Step 2: re-add them, and set their address entries
    string[] RecipentAddresses = { "test1", "testdl", "test2" };

    for (int i = 0; i < RecipentAddresses.Length; i++)
    {
        Outlook::Recipient recip = mailItem.Recipients.Add("test2");
        int addrType = recip.Type;
  
        Outlook::AddressEntry ae = Application.Session.AddressLists["Global Address List"].AddressEntries[RecipentAddresses[i]];
        recip.AddressEntry = ae;
       
        // need to set recipient type back to old value after re-adding
        recip.Type = addrType;
        recip.Resolve();
    }
    
    // Step 3: (only needed in some event callbacks)
    // Now addresses have changed, but sometimes the UI doesn't update until
    // we add and remove a final recipient.
    // This workaround may not be needed in the BeforeCheckNames event, but is
    // needed in some other events if updating the recipients

    mailItem.Recipients.Add("pleaseIgnore");
    mailItem.Recipients.Remove(mailItem.Recipients.Count);
}

Enjoy!