Microsoft Outlook Integration with C#.NET

Introduction

Interacting with Microsoft Office (as well as taking advantage of the mountains of built-in classes and functions) can be a powerful addition to your business applications. You can perform highly valued operations using the interoperability assemblies that ship with Office products to give the user an experience that will be familiar and easy to adopt. This article is the first part of a two-part series. It will focus on interacting with Outlook within the context of a .NET application. You will create a simple example to familiarize yourself with the Outlook interoperability classes, specifically dealing with the manipulation of Outlook contact items. It will also discuss briefly the security implications associated with Outlook interaction from your .NET application. Part Two of this series will cover extending Outlook via an add-in, illustrating how to add a custom button to the Outlook toolbar.

Redemption

The Redemption DLL is one way to avoid the security warnings introduced by the “Outlook Security Patch” included in Office 2002 and later when accessing information deemed to be sensitive. For example, when you try to access a contact’s email property later in this article, without Redemption when your application makes the call to Outlook for the information, Outlook displays the following message to the user:

If you do not mind inconveniencing your users with such warnings and plan to train them on the appropriate response to said dialogs, you can skip the rest of this section and all later references to Redemption. Less sadistic coders will be interested in the fact that the Redemption library exposes a method to access “sensitive” information without warnings. Redemption is available free for development purposes but requires a onetime licensing fee when distributing it with your applications. For more information about this library and to download the DLL, navigate here.

The source code sample included with this article does not use Redemption in case you choose not to download it; however, the code includes lines where Redemption could be used but are commented out. Also, note that no error catching is present in this sample code for the case when your user chooses to deny a security access request. You will want to add error checking in a real-world application. Redemption would avoid these scenarios.

If you decide to use Redemption, you will have to register the DLL on your computer by issuing the following command at a command prompt after navigating to the folder location of the DLL:

RegSvr32 Redemption.dll

You will also need to add a reference to the DLL in your solution before re-instating the code that uses Redemption to suppress the warnings.

Getting Started

Unlike Redemption, the Outlook COM library is a necessary reference for building an application that interacts with Outlook. For this article and its sample, I am using Visual Studio 2005 (.NET Framework 2.0), C#.NET, and Microsoft Office Outlook 2003. The first step is to add a reference to the appropriate Outlook COM library in your project. The Office interop libraries are located under the COM tab of the references dialog in Visual Studio. In my case, because I am using Outlook 2003, the library is called “Microsoft Outlook 11.0 Object Library.” These interop libraries ship with Office. Therefore, if you have Outlook 2003 installed on your machine, this is the library you will see. If you have Outlook 2007 installed, instead you will see “Microsoft Outlook 12.0 Object Library.”

In each class that will use the Outlook functionality, you will also want to include the following using directive:

using Outlook = Microsoft.Office.Interop.Outlook;

Please note that you may need to adjust the references in the sample project to run it with your machine’s configuration.

Creating a Simple Outlook Contact Management Application

To demonstrate some of the functionality of the Outlook interop class, you will create a “contact management” application that enables a user to choose an Outlook folder from which to view contacts, see the information for each contact when hovering over the name, and to alter some of the contacts’ properties. The sample will introduce you to the Outlook classes and how easy it can be to come up with your own uses for interacting with Outlook (and, by extension, Office) from your .NET applications.

The application, on first glance, will have an empty list box and a button to “Get Contacts” like so:

When the user clicks the button, he will be presented with an Outlook-style folder picker to choose the folder from which he wants to “import” contacts. The Outlook interop class exposes a PickFolder method of its Namespace class to provide this functionality with one line of code. First, however, you need to create an Outlook Application object that will expose the Outlook functionality and get its Namespace object.

Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS    = oApp.GetNamespace("MAPI");

“MAPI” is the supported parameter for the GetNamespace method with Outlook. The following code and screenshot illustrate how you accomplish the task of allowing the user to choose an Outlook folder.

private Outlook.MAPIFolder oContactsFolder = oNS.PickFolder();

You will limit the “import” process to contact types, so if the user chooses a folder with no contacts in it, he will receive no results. The PickFolder method returns an object of type MAPIFolder. Once you have a reference to the desired folder, you can iterate through its items. Before doing so, you will restrict the items to the contact type.

string filter = "[MessageClass] = \"IPM.Contact\""
Outlook.Items oContactItems = oContacts.Items.Restrict(filter);

foreach (Outlook.ContactItem oContact in oContactItems)
{}

More by Author

Must Read