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

24 Comments

  • https://secure.gravatar.com/avatar/e42f2a95052e4fa51fd9d50ff09a6525?s=32&d=https%3A%2F%2Fsecure.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.

  • https://secure.gravatar.com/avatar/46d0e8b43fb4bb64af04d8a881c1515b?s=32&d=https%3A%2F%2Fsecure.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.

  • https://secure.gravatar.com/avatar/273f99649863f87d2e2998a4168c9d6e?s=32&d=https%3A%2F%2Fsecure.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

  • https://secure.gravatar.com/avatar/96fcfeee9dd804c63df56d555e3f8bd4?s=32&d=https%3A%2F%2Fsecure.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?

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.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.

  • https://secure.gravatar.com/avatar/50caefbb4b419731f704864decfe83be?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Darrell Pelan says:

    Great code example. Solved a huge problem for me. Thanks!!

  • https://secure.gravatar.com/avatar/1a161929a91c56e43f7d84b54e36dd92?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Fernando says:

    hi

    it works fine but with a problem, it changes my bcdd to bcc do you have any idea why ?

    thanks

  • Hello Fernando,

    What is bcdd? What exactly occurs?

  • https://secure.gravatar.com/avatar/36868b32ef6f00f296993ca5c381065d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G vamsi says:

    Have to select a from address in outlook as I am having three mail addresses on which I have to work . please guide me on this issue.

  • Hello Vamsi,

    Select? The Outlook object model doesn’t provide a way to modify the selection in the To, CC, or BCC fields.

  • https://secure.gravatar.com/avatar/4a7f05ce016cee87ac160b7fa9bac541?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Henning Weltz says:

    Hi,
    I am working in VB6 and when I try to “Resolve” all my recipients, but do not want to display the email but simple send it regardless of whether the recipient is Resolved or not?
    Regards

  • Hello Helling,

    If the Recipients.ResolveAll() method returns false, call the Recipient.Delete() method for every Recipient object having Recipient.Resolved=True, then make sure there are recipients to send the email to, and send the email.

  • https://secure.gravatar.com/avatar/14a16e2a900a2f319b74cd6fee173fcd?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Aidan says:

    Hi

    Very useful post; I wonder if you could help with a problem I have, when I add an email address as a ‘CC’ recipient (olCC), this e-mail address is also being added as a ‘To’ recipient (olTo). Extract of the code is below, any thoughts on what I might be doing wrong?

    Aidan

    Set objOutlookRecipTO = .Recipients.Add(stremail)
    objOutlookRecipTO.Type = olTo

    Do Until intsub = intemailCC_count
    intsub = intsub + 1
    Set objOutlookRecipCC = .Recipients.Add(stremailCC(intsub))
    objOutlookRecipCC.Type = olCC
    Loop

  • Hello Aidan,

    I reproduce the issue in Outlook 2016. Still, it only occurs for me if I debug the code. When I run the code, the issue doesn’t occur.

    Sub dgdfggd()
    Dim m As Outlook.MailItem
    Set m = Application.ActiveInspector().CurrentItem
    Dim intsub As Integer
    intsub = 0
    Dim objOutlookRecipTO As Outlook.Recipient
    Dim objOutlookRecipCC As Outlook.Recipient
    Dim stremailCC() As String
    stremailCC = GetstremailCC()

    With m
    Set objOutlookRecipTO = .Recipients.Add(stremail)
    objOutlookRecipTO.Type = olTo
    objOutlookRecipTO.Resolve

    Do Until intsub = intemailCC_count
    intsub = intsub + 1
    Set objOutlookRecipCC = .Recipients.Add(stremailCC(intsub))
    objOutlookRecipCC.Type = olCC
    objOutlookRecipCC.Resolve
    Loop
    End With
    End Sub

    I start the macro by pressing F5.

  • https://secure.gravatar.com/avatar/14a16e2a900a2f319b74cd6fee173fcd?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Aidan says:

    Andrei

    Thank you very much for your time and efforts in trying to help me with my problem, but no matter how I run the routine, debug or not, I get the same result with the CC recipient being duplicated as a TO recipient.

    Regards

    Aidan

  • Hello Aidan,

    Try to install all updates on your Office. Turn all COM add-ins off. Make sure you have no macros reacting to Outlook events. What Office version are you using?

  • https://secure.gravatar.com/avatar/14a16e2a900a2f319b74cd6fee173fcd?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Aidan says:

    Hi Andrei – Office 2016 with Windows 10

  • Aidan,

    Are you saying turning *all* COM add-ins off doesn’t help? If so, I have no idea what to do next. Repair Office maybe?

  • https://secure.gravatar.com/avatar/667d8d8b30053fbcd8584627d1d3dcce?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G gabriel aguirre says:

    hi andrei

    i try to fill the cell “from” with a e-mail of my choice, in my work we have many sesion of outlook, and i need all email that we send say the same email adrees.

    i from venezuela my english is no so good. sorry for the failure.

  • Hello Gabriel,

    This isn’t something that the Outlook object model supports. It looks like you need to look for something in Outlook/Exchange settings. I suppose creating a shared mailbox may have some relation to the answer to your question.

  • https://secure.gravatar.com/avatar/7c84d4e9012023e6267d5bdbbd9c4492?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Justin Perry says:

    I can use this as VBScript in an Outlook form to modify the To box but when I add the line recipientTo.Type = Outlook.OlMailRecipientType.olTo I get an error that says object “Outlook” could not be found. Is there something I am doing wrong?

  • Hello Justin,

    The Outlook part in Outlook.OlMailRecipientType.olTo is an indication of the library that the enumeration OlMailRecipientType belongs to. I have never used VBScript but I have a dim remembrance that you need to use the value itself instead of referring to it using the notation above. I suggest that you try replacing Outlook.OlMailRecipientType.olTo with 1. I’ve found this value using the VBA object browser: In Outlook, press Alt+F11 to open the VBA IDE; when in the VBA IDE, press F2 to open the VBA object browser. HTH

Post a comment

Have any questions? Ask us right now!