For many occasions such as holidays or business events, I needed to send emails to my contacts without putting all the contact names in the To field of a single message. At many times, I needed to send a copy of a certain message to my contacts individually and I had to go through repeating the process of opening a message, pasting the body, entering the contact email and sending it.
Then I was introduced to Visual Studio Tools For Office (VSTO). I thought of creating an add-in for Outlook 2007 that reads my contacts from a database and sends a message to them one by one. This message will be entered in the Outlook editor as usual, including the subject and body. All that I have to do is press the Send button and this composed message is sent to my contacts stored previously in an Access database. This is not meant for SPAM since the sender email is retrieved from the current Outlook account and not faked by the user manually.
Please download the ZIP file and open the project in Visual Studio .NET 2008. Build and run. Outlook 2007 will be launched. To view the add-in, do the following:
- Open the contacts.mdb file located in the project folder and enter the information of your contacts. The most important data is the name and email address.
- Invoke the New Message Compose dialog.
- Select the CppMax Ribbon at the top left and then click the MiniCRM link below it to the right.
- A list of your customers (stored previously in contacts.mdb, located in the project folder) will appear in the left task pane.
- Enter the subject and body of your message.
- Click the Send Emails button at the bottom of the task pane below the customer list.
The Compose dialog will be closed and the new message will be sent to all recipients in the list.
To introduce yourself to the VSTO add-in, I suggest you watch the special webcast on Microsoft's VSTO site. I see this as a good starting point. The sample shown in this article was written after watching couple of those webcasts. I am not going to repeat what has been said in them, but I will highlight the steps needed to do add-ins for Outlook:
- Create the Project: File/Add/New Project. Select the Visual C# or VB.NET Office tree node. Select Outlook 2007 Add-in and enter the project name and path.
- Add a data source using Data/Add New Data Source. Select the database type and name in the wizard. Follow the wizard to select your fields and create the query.
- Create a new task pane. A task pane is a dockable window that appears to one side of the current Outlook window. A task pane is actually nothing more than a User Control. Deal with the User Control the same way you deal with a Windows Forms User Control. Add controls, events and .NET code to it as usual.
- Add a Ribbon: Project/Add/New Item/Ribbon XML. Edit the XML to specify your ribbon tab, group and buttons. Write the button event handlers to invoke the task pane or do anything you want within Outlook.
- Retrieve the data using the defined data source or through direct code and display the data within a grid or any data-bound control.
- This is all usual so far. What is different about this add-in is how it sends emails to all recipients one by one. Here is the code to do that:
Private contact As ContactsDataSet.ContactsRow
Private thisInspector As Outlook.Inspector
Private thisMessage As Outlook.MailItem
// The method handler to send the emails
Private Sub SendEmails_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles SendEmails.Click
thisInspector = Globals.ThisAddIn.Application.ActiveInspector
thisMessage = CType(thisInspector.CurrentItem, Outlook.MailItem)
Dim contact As ContactsDataSet.ContactsRow
Dim sSubject As String
Dim sBody As String
Dim msg As Outlook.MailItem
sSubject = thisMessage.Subject
sBody = thisMessage.Body
For Each contact In ContactsDataSet.Tables(0).Rows
msg = CType(Globals.ThisAddIn.Application.CreateItem(_
msg.Subject = sSubject
msg.Body = sBody
msg.To = contact.EmailAddress
- Note that the add-in is run from within Visual Studio .NET 2008 and not directly through a setup. This is because, to have a standalone final add-in, you need to sign it due to the strong security features in Office.
- A setup project is included to install the sample, but it may not run because the code needs to be authenticated and signed.
Much work needs to be done for this sample and it is not a final product. It is just to show you how. In the future, we may add Adding and Deleting to the Customer List directly from Outlook (and not just through Access). Also, importing from a text file is also possible.