gpt4 book ai didi

c# - 将 Outlook 联系人获取到基于 C# 表单的应用程序中

转载 作者:行者123 更新时间:2023-11-30 19:42:44 25 4
gpt4 key购买 nike

我曾尝试将 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();
}
}
}

最佳答案

这对我有用。它从 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);

}

关于c# - 将 Outlook 联系人获取到基于 C# 表单的应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16884063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com