Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Outlook contacts into C# form-based application

Tags:

c#

outlook

I have tried to get the contacts of Outlook contacts into C#, but it is not working. I have used the Microsoft Outlook 12.0 Object Library. I want to show the data in richtextbox or gridview.

The code is pasted below. Please let me know what I should do there.

    private void getContacts_Click(object sender, EventArgs e)
    {
        // Obtain an instance of the Outlook application
        Outlook.Application app = new Outlook.ApplicationClass();

        // Access the MAPI namespace
        Outlook.NameSpace ns = app.GetNamespace("MAPI");

        // Get the user's default contacts folder
        Outlook.MAPIFolder contacts =
        ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);

        // Iterate through each contact
        for (int i = 1; i < contacts.Items.Count + 1; i++)
        {
            // Get a contact
            Outlook.ContactItem contact =
            (Outlook.ContactItem)contacts.Items[i];
            richTextBox1.Text += contact.FullName + " (" +
            contact.BusinessTelephoneNumber + ")" + Environment.NewLine;
            Application.DoEvents();
        }
    }
}
like image 255
user2420211 Avatar asked Jun 02 '13 15:06

user2420211


2 Answers

This works for me. It gets all the contacts from outlook and shows it in datagridview.

  Microsoft.Office.Interop.Outlook.Items OutlookItems;
  Microsoft.Office.Interop.Outlook.Application outlookObj = new Microsoft.Office.Interop.Outlook.Application();
  MAPIFolder Folder_Contacts;
  Folder_Contacts = (MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
  OutlookItems = Folder_Contacts.Items;
  MessageBox.Show("Wykryto kontaktów: " + OutlookItems.Count.ToString());

  for (int i = 0; i < OutlookItems.Count; i++)
  {
    Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i+1];
    sNazwa = contact.FullName;
    sFirma = contact.CompanyName;
    sAdress = contact.BusinessAddressStreet;
    sMiejscowosc = contact.BusinessAddressPostalCode + " " + contact.BusinessAddressCity;
    sEmail = contact.Email1Address;
    dataGridView1.Rows.Add(sNazwa, sFirma, sAdress, sMiejscowosc, sEmail);

  }
like image 156
Łukasz Motyczka Avatar answered Sep 24 '22 23:09

Łukasz Motyczka


I have tried the below-mentioned code to fetch the data from Outlook to C# desktop application in gridview. I have the above-mentioned API for that and got the email address of Outlook that is configured on your system! The code is pasted below.

The used API works fine with outlook 2007 and 2003, but for outlook 2010, it's suggested to use the other API.

public partial class Form1: Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        fetchOutlookContacts();
    }

    public void fetchOutlookContacts()
    {
        Microsoft.Office.Interop.Outlook.Items OutlookItems;
        Microsoft.Office.Interop.Outlook.Application outlookObj;
        MAPIFolder Folder_Contacts;

        outlookObj = new Microsoft.Office.Interop.Outlook.Application();
        Folder_Contacts = (MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
        OutlookItems = Folder_Contacts.Items;

        DataTable dt = new DataTable();
        dt.Columns.Add("Email Address");

        for (int i = 0; i < OutlookItems.Count; i++)
        {
            Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i + 1];
            dt.Rows.Add(new object[] { contact.Email1Address });

        }
        dataGridView1.DataSource = dt;
        dataGridView1.Show();

    }
}
like image 38
user2420211 Avatar answered Sep 24 '22 23:09

user2420211