5

我试图将 Outlook 联系人的联系人转换为 C#,但它不起作用。我使用了 Microsoft Outlook 12.0 对象库。我想在richtextbox 或gridview 中显示数据。

代码粘贴在下面。请让我知道我应该在那里做什么。

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

4 回答 4

7

这对我有用。它从 Outlook 中获取所有联系人并将其显示在 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);

  }
于 2013-06-13T09:35:00.840 回答
3

我已经尝试使用下面提到的代码将数据从 Outlook 提取到gridview. 我有上面提到的 API,并获得了您系统上配置的 Outlook 电子邮件地址!代码粘贴在下面。

使用的 API 在 Outlook2007和中运行良好2003,但对于 Outlook 2010,建议使用其他 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();

    }
}
于 2013-06-13T09:25:23.927 回答
2

此代码在我的 C#-Solution 中运行良好。

using Outlook =Microsoft.Office.Interop.Outlook;

private void kontaktImport_Click(object sender, RoutedEventArgs e)
        {
            Outlook.Application app = new Outlook.Application();
            Outlook.NameSpace NameSpace = app.GetNamespace("MAPI");
            Outlook.MAPIFolder ContactsFolder = NameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
            Outlook.Items ContactItems = ContactsFolder.Items;
            try
            {
                foreach (Outlook.ContactItem item in ContactItems)
                {
                    String output = "";
                    output = item.FirstName + "\n";
                    output += item.LastName;
                    TestTextBox.Text = output;
                }
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                TestTextBox.Text = ex.ToString();
            }
        }         
于 2017-08-26T21:12:00.030 回答
1

好吧,Microsoft Interop 有一个解决方案,但这会给未安装 Microsoft Office 的系统带来问题。所以我用微软交换服务解决了这个问题。现在我已经在 Asp.Net mvc 中尝试过了,它工作正常。这就是您在 Asp.Net 中获取联系人的方式

public void GetContact()
    {
        
        string ewsUrl = "https://outlook.office365.com/EWS/Exchange.asmx";
        string userName = "outlookusername";
        string password = "outlookpassword";

        ExchangeService servicex = new ExchangeService();
        servicex.Url = new Uri(ewsUrl);
        servicex.UseDefaultCredentials = true;
        servicex.Credentials = new WebCredentials(userName, password);
        ContactsFolder contactsfolder = ContactsFolder.Bind(servicex, WellKnownFolderName.Contacts);
        int numItems = contactsfolder.TotalCount < 50 ? contactsfolder.TotalCount : 50;
        if (numItems == 0)
            AddContact();
        numItems = contactsfolder.TotalCount < 50 ? contactsfolder.TotalCount : 50;
        // Instantiate the item view with the number of items to retrieve from the Contacts folder.
        ItemView view = new ItemView(numItems);

        // To keep the request smaller, request only the display name property.
        view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ContactSchema.DisplayName);

        // Retrieve the items in the Contacts folder that have the properties that you selected.
        FindItemsResults<Item> contactItems = servicex.FindItems(WellKnownFolderName.Contacts, view);

        // Display the list of contacts. 
        foreach (Item item in contactItems)
        {
            if (item is Contact)
            {
                Contact contact = item as Contact;
                Console.WriteLine(contact.DisplayName);
            }
        }
    }`

你只需要在那里替换outlook用户名密码,这会给你联系方式。如果没有联系人,我调用了添加联系人方法来添加联系人。有关更多参考,您可以查看这篇文章,我发现 Microsoft Outlook 添加联系人并使用 Microsoft Exchange 服务获取联系人 Asp.Net MVC

于 2020-10-15T04:13:50.687 回答