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!
31 Comments
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 https://www.ablebits.com/outlook-blind-carbon-copy-bcc-email-addins/index.php.
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.
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
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?
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.
Great code example. Solved a huge problem for me. Thanks!!
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?
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.
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.
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.
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?
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?
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.
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
I have a related question/issue: I am able to use VBA to send e-mails with Access with no problem. However, I want to take it one step further. I use various templates that have static To and CC recipients.
However, each email that gets sent out that needs to have a point of contact added at the end of the CC recipients.
If need be, I can post the code I am using…
Thanks in advance! John
Hello John,
At https://msdn.microsoft.com/en-us/library/office/ff866951(v=office.15).aspx they show how to add a recipient to the To field. You need to use https://msdn.microsoft.com/en-us/library/office/ff863703(v=office.15).aspx to specify that the recipient is added to the CC field. After this is done, you may want to resolve the recipient: call Recipient.Resolve().
Sir, I am creating a screen to display To, CC, BCC, Subject, and Attachments.
My question to you is How to give the attachment file a link so that we can download and check before sending.
if (olMailItem.Attachments.Count > 0)
{
foreach(Outlook.Attachment attachment in olMailItem.Attachments)
{
totalSize = attachment.Size/1024;
FillUpValueInRow(“??????”, attachment.FileName + ” (” + totalSize + ” KB)”);
}
}
====================================================================================
The above is my code for the attachment part
Hello Pratima,
>How to give the attachment file a link so that we can download
Typically, this is solved in this way: you upload the attached file to a server, get a link, delete the attachment from the email and update the email body so that it contains the link.
[…] How To: Fill TO,CC and BCC fields in Outlook programmatically […]
[…] Be aware, to preserve the message body from the original email you need to insert your content between the opening <body> and closing </body> tags. If you need to add in the beginning of the message paste your additional text right after the opening tag, if you intend to paste it in the end of message – paste right before the closing tag. Also you may find the Recipients property of the MailItem class helpful. It allows a more convenient way for setting up recipients for the Outlook items. You can read more about that property in the article that I wrote for the technical blog – How To: Fill TO,CC and BCC fields in Outlook programmatically. […]