I am trying to import the list of email addresses from Offline address list to a combo box, it seems to work fine in finding the List however, everytime I try to pull the data, it freezes the whole app, and when putting it into the Form_Load, the app refuses to come up.
I tried to Change to a textbox and issue remained the same. When trying a false address list, the app throughs an error that it can not find the list
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.Accounts accounts = app.Session.Accounts;
Microsoft.Office.Interop.Outlook.NameSpace oNS = app.GetNamespace("mapi");
Microsoft.Office.Interop.Outlook.AddressLists oDLs = oNS.AddressLists;
Microsoft.Office.Interop.Outlook.AddressList oGAL = oDLs["Offline Global Address List"];
foreach (AddressEntry item in oGAL.AddressEntries)
{
comboBox1.Items.Add(item.Address);
}
}
To summarize the discussion in the comments:
Never loop through all items in an address book container (or a folder for that matter).
If you need to resolve a name, use Namespace.CreateRecipient
/ Recipient.Resolve
.
Once the recipient is resolved, do not use ToString()
method - it is implemented by the .Net wrapper, not by the object itself. Use the property that will return what you need - Recipient.Name
. Or Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress
, etc.