Outlook Integration in C#

This article shows how to integrate the outlook from visual studio. We can do the manipulation with outlook with c# language. We can create contacts in outlook contacts, tasks in outlook tasks from visual studio. Microsoft provides the Com components libraries. To complete this task The Microsoft Outlook 11.0 Outlook Library reference should be added to our project from com components tab. When we add a reference to our project the Microsft.Office.Core and Outlook assemblies will be added.

 

The outlook namespace consists of all outlook classes like application, contact item, task item, appointment item, and Journal item. Every class deals with the folders in outlook application. In this article, we are going to create contact in default contact folder. In the same way we can create tasks, appointment etc in outlook from visual studio with c# code.

A brief introduction to outlook.
           
.pst File

Microsoft Outlook (non-Exchange Server) uses files with the extension .pst to store your e-mail messages, calendar, contacts, and other information to your computer. These files mobilize you to restore data that is lost or damaged because of a hardware failure and move or transfer data to a different hard disk drive.

MapiFolders

Outlook uses MAPI folders to keep track of your e-mail messages, contacts, appointments, tasks, notes, and journal entries. Outlook keeps these files in one of two places, depending on the type of e-mail server you use. Your MAPI file is either in a personal storage folder (.pst) on your hard disk drive or in a mailbox located on the server, if you are using Outlook with Microsoft Exchange server.

For accessing those MAPI folders first we have to instantiate the Outlook Application object. We are referring the outlook name space couple of times .so, rename outlook namespace rename like this.

  1. using OutLook = Microsoft.Office.Interop.Outlook;  
First we have to create outlook application interface object.
  1. OutLook._Application outlookObj = new OutLook.Application();  
As outlook uses mapi folders to keep contacts, we have to loop through all outlook folders and get the outlook contact folder.
  1. OutLook.MAPIFolder fldContacts = (OutLook.MAPIFolder)outlookObj.Session.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderContacts);  
Then, we have to instantiate outlook contact item and add to the outlook contacts folder as below.
  1. OutLook.ContactItem newContact = (OutLook.ContactItem)fldContacts.Items.Add(OutLook.OlItemType.olContactItem);  
Get the properties of contact item as shown below.
  1. newContact.FirstName = txtFirstName.Text.Trim().ToString();  
  2. newContact.LastName = txtLastName.Text.Trim().ToString();  
  3. newContact.Email1Address = txtEmail.Text.Trim().ToString();  
  4. newContact.Business2TelephoneNumber = txtPhone.Text.Trim().ToString();  
  5. newContact.BusinessAddress = txtAddress.Text.Trim().ToString();  
In our example the contact properties are getting from text box values .so the text box values are assigning to the outlook contact item. These values we can get from database, excel sheet or Web service.

We can save this new contact item by invoking the save method on contact item object.

  1. newContact.Save();  
The contact item will be created in default contacts folder in outlook.

If we need to create a custom folder in default contacts folder of outlook application, we can follow below steps.

First we have to check whether the folder exists or not and if not create new folder as like this.
  1. public bool CheckCustomFolderExisits()  
  2. {  
  3.     OutLook._Application outlookObj = new OutLook.Application();  
  4.     OutLook.MAPIFolder fldContacts = (OutLook.MAPIFolder)outlookObj.Session.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderContacts);  
  5.     //VERIFYING THE CUSTOM SUB FOLDER IN CONTACTS FOLDER IN OUT LOOK.  
  6.     foreach (OutLook.MAPIFolder subFolder in fldContacts.Folders)  
  7.     {  
  8.         if (subFolder.Name == "CustomeFolderName")  
  9.         {  
  10.             CustomFolder= subFolder;  
  11.             return true;  
  12.         }  
  13.         else  
  14.             return false;  
  15.     }  
  16.     return false;  
  17. }  
For verifying the existing folder we have to loop through all folders in contacts folder

If not create folder in default contacts folder as below.

  1. public void CreateCustomFolder()  
  2. {  
  3.     OutLook._Application outlookApp = new OutLook.Application();  
  4.     OutLook.MAPIFolder cntctFolder =(OutLook.MAPIFolder)outlookApp.Session.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderContacts);  
  5.     //VERIFYING THE CUSTOM FOLDER IN OUT LOOK .  
  6.     foreach (OutLook.MAPIFolder subFolder in cntctFolder.Folders)  
  7.     {  
  8.         if (subFolder.Name == " CustomeFolderName ")  
  9.         {  
  10.             CustomFolder= subFolder;  
  11.         }  
  12.     }  
  13.     //IF  CUSTOM FOLDER DOES NOT EXIST CREATE A NEW FOLDER WITH NAME  CUSTOM FOLDER NAME.  
  14.     if (CustomFolder == null)  
  15.     {  
  16.         CustomFolder = contactsFolder.Folders.Add("CustomeFolderName ", Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);  
  17.     }  
  18. }  
We can create contacts in this folder as u follow steps as same as in creating the contacts in default folder, by passing our custom folder as the parameter. When we are creating the contact in default contacts folder it creates the contact item with unique entry id. We can cross verify the contacts by this entry id.

I want to add custom properties to contact item the contact item has a list property called user properties where we can add custom properties to identify the contact.

In our code example, the grid shows the contacts in different folders in outlook application. The folders in our outlook application are displayed in a combo.

This code gets the contacts from contacts folder in outlook application.

  1. foreach (Microsoft.Office.Interop.Outlook._ContactItem contactItem in fldContacts.Items)  
  2. {  
  3.     MyContact contact = new MyContact();  
  4.     contact.FirstName = (contactItem.FirstName == null) ? string.Empty:contactItem.FirstName;  
  5.     contact.LastName = (contactItem.LastName == null) ? string.Empty :contactItem.LastName;  
  6.     contact.EmailAddress = contactItem.Email1Address;  
  7.     contact.Phone = contactItem.Business2TelephoneNumber;  
  8.     contact.Address = contactItem.BusinessAddress;  
  9.     contacts.Add(contact);  
  10. }

The code example allows creating a contact in default folder or custom folder by selecting the custom folder option. The custom Property added in example is 'myPetName'. We can add custom property to contact item like this.

Notice here, the user properties takes the outlookuserpropertytype as parameter.

  1. newContact.UserProperties.Add("myPetName",OutLook.OlUserPropertyType.olText, true, OutLook.OlUserPropertyType.olText);  
  2. newContact.UserProperties["myPetName"].Value = txtProp1.Text.Trim().ToString();  
When we check verify the contact with custom property if it exists it updates the old contact or it creates a new contact.  

Here in this method, it finds the existed contact with user property value.

  1. private OutLook._ContactItem FindContactItem(MyContact contact, OutLook.MAPIFolder folder)  
  2. {  
  3.     object missing = System.Reflection.Missing.Value;  
  4.     foreach (OutLook._ContactItem contactItem in folder.Items)  
  5.     {  
  6.         OutLook.UserProperty userProperty = contactItem.UserProperties.Find("myPetName", missing);  
  7.         if (userProperty != null)  
  8.         {  
  9.             if (userProperty.Value.Equals(txtProp1.Text.Trim().ToString()))  
  10.                return contactItem;  
  11.         }  
  12.      }  
  13.      return null;  
  14. } 

 

Outlook Security Access

When we are accessing the outlook contacts the outlook application will prompt for message as shown.

It is a safeguard Microsoft put in to help prevent viruses from mailing everyone in your contacts in outlook. We allow access for 1 minute or more than one minute if there are lots of contacts.

The sample attached can be downloaded and you can learn how the code works.

Note: VS 2005 and Outlook is required to run this application.


Similar Articles