将 Outlook 联系人获取到基于 C# 表单的应用程序中 [英] Get Outlook contacts into C# form-based application

查看:20
本文介绍了将 Outlook 联系人获取到基于 C# 表单的应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将 Outlook 联系人的联系人导入 C#,但它不起作用.我使用了 Microsoft Outlook 12.0 对象库.我想在 Richtextbox 或 gridview 中显示数据.

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

推荐答案

这对我有用.它从 Outlook 中获取所有联系人并将其显示在 datagridview 中.

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

  }

这篇关于将 Outlook 联系人获取到基于 C# 表单的应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆