我正在尝试使用this解决方案调用方法DisplayAccountInformation()
。
public partial class OutlookContacts : Form
{
public OutlookContacts()
{
InitializeComponent();
//DisplayAccountInformation(??);
}
public static void DisplayAccountInformation(Outlook.Application application)
{
// The Namespace Object (Session) has a collection of accounts.
Outlook.Accounts accounts = application.Session.Accounts;
// Concatenate a message with information about all accounts.
var builder = new StringBuilder();
// .....
// [more code]
// .....
// Display the account information.
System.Windows.Forms.MessageBox.Show(builder.ToString());
}
}
我正在尝试获取联系人列表,包括用户名和电子邮件地址。
这是一个C#Windows窗体应用程序。
如何调用方法DisplayAccountInformation()
?
答案 0 :(得分:0)
请改用Application.Session.AddressLists
。您可以使用OutlookSpy中的对象进行播放:单击“命名空间”按钮,选择“地址列表”属性,单击“浏览”,转到IEnumVariant选项卡。等
答案 1 :(得分:0)
经过一些研究并通过微软关于Outlook的文档,我找到了一个解决方案here,它可以完成我想要的工作。现在我很清楚" Session"的错误是什么。 @Dmitry Streblechenko,@ Yacoub Massad你使用Application.Session.AddressLists
是正确的。
以下是我的代码:
public partial class OutlookContacts : Form
{
private BindingSource supplierDataBindingSource = new BindingSource();
public static IEnumerable<Employees> displayListOfOutLookUsers = new List<Employees>();
public OutlookContacts()
{
InitializeComponent();
dataGridView1.DataSource = supplierDataBindingSource;
displayListOfOutLookUsers = EnumerateGAL();
}
private IEnumerable<Employees> EnumerateGAL()
{
Outlook.Application ol = new Outlook.ApplicationClass();
/*Outlook.AddressList gal = Application.Session.GetGlobalAddressList(); won't work because '_Application' needs to be instantiated using an object before using. */
Outlook.AddressList gal = ol.Session.GetGlobalAddressList();
/*Declaring a list emp of type Employees.*/
List<Employees> emp = new List<Employees>();
if (gal != null)
{
for (int i = 1;
i <= Math.Min(100, gal.AddressEntries.Count - 1); i++)
{
Outlook.AddressEntry addrEntry = gal.AddressEntries[i];
if (addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry
|| addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
{
Outlook.ExchangeUser exchUser =addrEntry.GetExchangeUser();
/*Storing fetched records in the list.*/
emp.Add(new Employees {EmployeeName = exchUser.Name,EmployeeEmail = exchUser.PrimarySmtpAddress});
}
if (addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry)
{
Outlook.ExchangeDistributionList exchDL = addrEntry.GetExchangeDistributionList();
}
}
}
displayListOfOutLookUsers = emp;
supplierDataBindingSource.DataSource = displayListOfOutLookUsers.Select(l => new { l.EmployeeName, l.EmployeeEmail });
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.DisplayedCells);
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
int j = 0;
foreach (DataGridViewColumn c in dataGridView1.Columns)
{
c.SortMode = DataGridViewColumnSortMode.Automatic;
j += c.Width;
}
dataGridView1.Width = j + dataGridView1.RowHeadersWidth + 232;
this.Width = dataGridView1.Width + 40;
return displayListOfOutLookUsers;
}
在OutlookContacts形式中,我有一个datagridview,我想用所有Exchange用户的名称和电子邮件地址填充它。