Get Outlook contacts into C# form-based application

前端 未结 4 703
温柔的废话
温柔的废话 2020-12-06 19:29

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 richte

4条回答
  •  伪装坚强ぢ
    2020-12-06 20:06

    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();
    
        }
    }
    

提交回复
热议问题