Eugene Astafiev

How To: Fill TO,CC and BCC fields in Outlook programmatically

In my previous article, where I showed how to create and send an Outlook message programmatically, I added a recipient to the e-mail. Now I want to delve deeper and show you what you can do with the Recipients collection. Outlook developers frequently need to add recipients to the TO, CC and BCC fields. In today’s lesson we will iterate over all recipients and delete them first. Then we will add new ones to the TO,CC and BCC fields of a mail item. So, let’s go ahead!

The Outlook Recipients collection provides the following vital methods:

  • Add – creates a new Recipient in the Recipients collection. It accepts the recipient’s name or the full SMTP e-mail address and returns a newly created instance of the Recipient class, which was added to the collection. You can read more about using this method in my post on How to create and send an Outlook message programmatically.
  • Item – returns a recipient object from the collection by index or recipient name.
  • Remove – removes a recipient from the collection by index.
  • ResolveAll – the most often used method that attempts to resolve recipients from the collection against the address book. It returns false if one or more recipients were not resolved.

The Recipient class in Outlook provides a lot of useful properties and methods. The most important are:

  • Delete method – deletes a particular recipient form the collection.
  • Name property – returns a display name for the recipient. Read-only.
  • Resolve method – attempts to resolve a recipient object against the address book. It returns true if the recipient was resolved.
  • Resolved property – returns true if the recipient was validated against the address book. Read-only.
  • Type property – returns or sets an integer value representing the recipient type.

In the table below I attempted to show you enumerations and values for various Outlook items that you can set for the Type property.

Outlook items Enumeration Values
JournalItem OlJournalRecipientType olAssociatedContact
MailItem OlMailRecipientType olBCC, olCC, olOriginator, olTo
TaskItem OlTaskRecipientType olFinalStatus, olUpdate
MeetingItem OlMeetingRecipientType olOptional, olOrganizer, olRequired, olResource

In the sample code, all the recipients from the collection are removed and new ones are added. Then I set the To, CC and BCC fields for an Outlook mail item. As you can see, the appropriate values from the OlMailRecipientType enumeration are used for this task. The AddRecipients method accepts an instance of the MailItem object and then does its job.

C# and Add-in Express:

private bool AddRecipients(Outlook.MailItem mail)
{
    bool retValue = false;
    Outlook.Recipients recipients = null;
    Outlook.Recipient recipientTo = null;
    Outlook.Recipient recipientCC = null;
    Outlook.Recipient recipientBCC = null;
    try
    {
        recipients = mail.Recipients;
        // first, we remove all the recipients of the e-mail
        while(recipients.Count != 0)
        {
           recipients.Remove(1);                    
        }
        // now we add new recipietns to the e-mail
        recipientTo = recipients.Add("Eugene Astafiev");
        recipientTo.Type = (int)Outlook.OlMailRecipientType.olTo;                
        recipientCC = recipients.Add("Dmitry Kostochko");
        recipientCC.Type = (int)Outlook.OlMailRecipientType.olCC;
        recipientBCC = recipients.Add("eugene.astafiev@somedomain.com");
        recipientBCC.Type = (int)Outlook.OlMailRecipientType.olBCC;
        retValue = recipients.ResolveAll();
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
        if (recipientBCC != null) Marshal.ReleaseComObject(recipientBCC);
        if (recipientCC != null) Marshal.ReleaseComObject(recipientCC);
        if (recipientTo != null) Marshal.ReleaseComObject(recipientTo);
        if (recipients != null) Marshal.ReleaseComObject(recipients);
    }
    return retValue;
}

VB.NET and Add-in Express:

Private Function AddRecipients(mail As Outlook.MailItem) As Boolean
    Dim retValue As Boolean = False
    Dim recipients As Outlook.Recipients = Nothing
    Dim recipientTo As Outlook.Recipient = Nothing
    Dim recipientCC As Outlook.Recipient = Nothing
    Dim recipientBCC As Outlook.Recipient = Nothing
    Try
        recipients = mail.Recipients
        ' first, we remove all the recipients of the e-mail
        While recipients.Count > 0
            recipients.Remove(1)
        End While
        ' now we add new recipietns to the e-mail
        recipientTo = recipients.Add("Eugene Astafiev")
        recipientTo.Type = Outlook.OlMailRecipientType.olTo
        recipientCC = recipients.Add("Dmitry Kostochko")
        recipientCC.Type = Outlook.OlMailRecipientType.olCC
        recipientBCC = recipients.Add("eugene.astafiev@somedomain.com")
        recipientBCC.Type = Outlook.OlMailRecipientType.olBCC
        retValue = recipients.ResolveAll()
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    Finally
        If Not IsNothing(recipientBCC) Then Marshal.ReleaseComObject(recipientBCC)
        If Not IsNothing(recipientCC) Then Marshal.ReleaseComObject(recipientCC)
        If Not IsNothing(recipientTo) Then Marshal.ReleaseComObject(recipientTo)
        If Not IsNothing(recipients) Then Marshal.ReleaseComObject(recipients)
    End Try
    Return retValue
End Function

C# and VSTO:

using System.Runtime.InteropServices;
// ...
private bool AddRecipients(Outlook.MailItem mail)
{
    bool retValue = false;
    Outlook.Recipients recipients = null;
    Outlook.Recipient recipientTo = null;
    Outlook.Recipient recipientCC = null;
    Outlook.Recipient recipientBCC = null;
    try
    {
        recipients = mail.Recipients;
        // first, we remove all the recipients of the e-mail
        while(recipients.Count != 0)
        {
            recipients.Remove(1);                    
        }
        // now we add new recipietns to the e-mail
        recipientTo = recipients.Add("Eugene Astafiev");
        recipientTo.Type = (int)Outlook.OlMailRecipientType.olTo;
        recipientCC = recipients.Add("Dmitry Kostochko");
        recipientCC.Type = (int)Outlook.OlMailRecipientType.olCC;
        recipientBCC = recipients.Add("eugene.astafiev@somedomain.com");
        recipientBCC.Type = (int)Outlook.OlMailRecipientType.olBCC;
        retValue = recipients.ResolveAll();
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
        if (recipientBCC != null) Marshal.ReleaseComObject(recipientBCC);
        if (recipientCC != null) Marshal.ReleaseComObject(recipientCC);
        if (recipientTo != null) Marshal.ReleaseComObject(recipientTo);
        if (recipients != null) Marshal.ReleaseComObject(recipients);
    }
    return retValue;
}

VB.NET and VSTO:

Imports System.Runtime.InteropServices
' ...
Private Function AddRecipients(mail As Outlook.MailItem) As Boolean
    Dim retValue As Boolean = False
    Dim recipients As Outlook.Recipients = Nothing
    Dim recipientTo As Outlook.Recipient = Nothing
    Dim recipientCC As Outlook.Recipient = Nothing
    Dim recipientBCC As Outlook.Recipient = Nothing
    Try
        recipients = mail.Recipients
        ' first, we remove all the recipients of the e-mail
        While recipients.Count > 0
            recipients.Remove(1)
        End While
        ' now we add new recipietns to the e-mail
        recipientTo = recipients.Add("Eugene Astafiev")
        recipientTo.Type = Outlook.OlMailRecipientType.olTo
        recipientCC = recipients.Add("Dmitry Kostochko")
        recipientCC.Type = Outlook.OlMailRecipientType.olCC
        recipientBCC = recipients.Add("eugene.astafiev@somedomain.com")
        recipientBCC.Type = Outlook.OlMailRecipientType.olBCC
        retValue = recipients.ResolveAll()
    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    Finally
        If Not IsNothing(recipientBCC) Then Marshal.ReleaseComObject(recipientBCC)
        If Not IsNothing(recipientCC) Then Marshal.ReleaseComObject(recipientCC)
        If Not IsNothing(recipientTo) Then Marshal.ReleaseComObject(recipientTo)
        If Not IsNothing(recipients) Then Marshal.ReleaseComObject(recipients)
    End Try
    Return retValue
End Function

See you on our forums and in the e-mail support!

You may also be interested in:

HowTo: Deal with the Recipients collection in Outlook

7 Comments

  • http://0.gravatar.com/avatar/e42f2a95052e4fa51fd9d50ff09a6525?s=32&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ash says:

    Hi,

    I have created an email form within outlook and want to automatically populate the BCC field with the sender’s address so they receive a copy of the form they submitted. I have searched the internet but can’t find a way to get it to work. Is there some modification I could make to the above to make it work?

    Thanks,
    Ash

  • Hello Ash,

    The above demonstrates the idea how to solve the task in code. You may need to translate the above to the programming language that you use. If however you are asking about an end-user solution, then please check Auto BCC for Microsoft Outlook at http://www.ablebits.com/outlook-blind-carbon-copy-bcc-email-addins/index.php.

  • http://0.gravatar.com/avatar/46d0e8b43fb4bb64af04d8a881c1515b?s=32&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dav says:

    Hello Andrei,

    I have a datagrid where I am showing contact list of our people with their individual email address/s. I have added a checkbox.
    I want to check if outlook new message is open if it is open then add all selected email address (datagrid cell value) to the BCC field.

    Can you please guide me with this?
    thanks

  • Hello Dav,

    A composed email is identified by MailItem.Sent returning false; a newly created non-saved email is identified by MailItem.EntriID returning an empty string. Adding recipients and resolving them is shown above. You may want to resolve all recipients before and after adding new ones.

  • http://0.gravatar.com/avatar/273f99649863f87d2e2998a4168c9d6e?s=32&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Tammy Erickson says:

    Dear Mr. Astafiev,
    December 2013 you replied to a query I posted
    (on another forum page) about Outlook email
    templates and I am late thanking you. Please
    excuse me.
    Thank you for taking the time to reply to my query.
    Kindly,
    Tammy Erickson

  • http://1.gravatar.com/avatar/96fcfeee9dd804c63df56d555e3f8bd4?s=32&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ganesh says:

    Is it possible to add images next to addresses in to and cc? Similar to images added by Microsoft lync? Is it possible to format the addresses e.g. Using strike through or bold font?

  • http://0.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    Hi Ganesh,

    The Outlook Object Model does not provide any way for developers to implement either of your tasks. I suppose the first task, regarding “images” for recipients, can be done somehow but I don’t know what API allows doing this.

Post a comment

Have any questions? Ask us right now!