Page 1 of 1

working with Outlook Contacts

#1 agent1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 73
  • Joined: 09-July 10

Posted 18 November 2010 - 07:30 AM

if your wanting to work with your outlook contacts programatically then this article should help you get started..

First off you need to have Microsoft Outlook installed on your development computer.
You will need to add a reference to the Microsoft Outlook Object Library.
You can do that in solution explorer by right-clicking references / add reference… under the COM tab you will find whatever version of the object library you have on your pc…

Add this Namespace to your code file:

using Outlook = Microsoft.Office.Interop.Outlook



for this example I'm going to create a method to get all the contacts in Outlooks default folder and write them to the console,you can easily access subfolders of a given contacts folder and I will show you how to do that shortly…

For simplicities sake I’m going to create a class called Contact to represent a single contact…
public class Contact
{
public string Name { get; set; }
public string Email { get; set; }
}



and here's our method

public void GetOutlookContacts()
{
//create a new list to add contact items to
List<Contact> contacts = new List<Contact>();
try
{
//initialise an instance of Outlook object
Outlook._Application outLookApp = new Microsoft.Office.Interop.Outlook.Application();
//Get the default Contacts folder
Outlook.MAPIFolder defaultContactsFolder = outLookApp.Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderContacts);

//to access the subfolders of this folder you can use its Folders property
//defaultContactsFolder.Folders

}
catch
{
Console.Writeline("Error instantiating outlook");
}


//loop through all the contact items in the folder and add them to our list
foreach (Outlook.ContactItem contact in contactsFolder.Items)
{
Contact contactToAdd = new Contact
{
Name = contact.FullName,
Email = contact.Email1Address
};

//add them to the list
contacts.Add(contactToAdd);
}

//write them out to the console
foreach(Contact contact in contacts)
{
  Console.Writeline(String.Format("{0} - {1}",contact.Name,contact.Email));
}

}



and there you have it, you can do anything with those contacts now....

Is This A Good Question/Topic? 0
  • +

Page 1 of 1