0

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);
        }
    }
majdinuk
  • 13
  • 3
  • Does it freeze after processing a few hundred items? Why would you want to read **all** addresses from GAL? – Dmitry Streblechenko Jul 17 '19 at 16:52
  • it freezes the second the button is clicked, I need it in order to make a user directory to search for users within the app – majdinuk Jul 17 '19 at 16:54
  • So which call freezes? If you want to resolve a name, use Namespace.CreateRecipeint / Recipient.Resolve. Downloading all GAL entries is like downloading the whole SQL database into a text file when all you want is a single row. – Dmitry Streblechenko Jul 17 '19 at 17:08
  • I cant see anything in the call stack!, all I need to get is all the email addresses off the GAL either online or offline? I thought trying to pull it from the offline GAL would make it quicker, however unfortunately that had no effect, and the problem is that the GAL contains around 80K contacts – majdinuk Jul 17 '19 at 17:29
  • Yes, don't do that. Use Namespace.CreateRecipeint / Recipient.Resolve. – Dmitry Streblechenko Jul 17 '19 at 19:09
  • thank you very much both for your help, I have followed your advise by amending the code to the following: "Recipient r = oGAL.Session.CreateRecipient(textBox1.Text); r.Resolve();comboBox1.Text = r.ToString(); and now the combobox comesup with System_ComObject result? – majdinuk Jul 19 '19 at 15:35
  • COM objects are not .Net objects, they do not implement ToString() method. Use the property that you need - Recipient.Name. Or Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress, etc. – Dmitry Streblechenko Jul 19 '19 at 16:36

2 Answers2

0

GAL contains around 80K contacts

Iterating through all items in the address book is not really a good idea:

foreach (AddressEntry item in oGAL.AddressEntries)
{
   comboBox1.Items.Add(item.Address);
}

The Outlook object doesn't provide any methods for searching address entries. But the best what you can do is to try to Resolve the name or address against the address book.

The name of the recipient can be a string representing the display name, the alias, or the full SMTP email address of the recipient.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • thank you very much both for your help, I have followed your advise by amending the code to the following: "Recipient r = oGAL.Session.CreateRecipient(textBox1.Text); r.Resolve();comboBox1.Text = r.ToString(); and now the combobox comesup with System_ComObject result? – majdinuk Jul 19 '19 at 14:52
0

To summarize the discussion in the comments:

  1. Never loop through all items in an address book container (or a folder for that matter).

  2. If you need to resolve a name, use Namespace.CreateRecipient / Recipient.Resolve.

  3. 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.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • That has solved it thank you so much, I was hoping to get an auto complete for all the contacts but that seems to be a bit too heavy to load, your method worked like a charm – majdinuk Jul 21 '19 at 01:25
  • OOM won't let you do autocomplete - if using Redemption is an option, it can be used on a secondary thread and implements RDOSession.AddressBook.ResolveNameEx - it returns a lists of matches for a given name. – Dmitry Streblechenko Jul 21 '19 at 03:44