'How to get SMTP address for exchange user quickly?

The below is my code, the PrimarySmtpAddress use 50ms which need more than 15 s to load more 300 users. I am not familiar with Outlook API, and it seems this Property is the usual way to retrieve the SMTP address, but it is far too slow.

Is there other way to retrieve the SMTP address or I use this property incorrectly?

The .NetFramework is 3.5

Outlook Version is 2010

Microsoft.Office.Interop.Outlook is 12.0.0.0

Microsoft.Office.Interop.Outlook.NameSpace olNS = outlook.GetNamespace("MAPI");
olNS.Logon(null, null, false, true);

Microsoft.Office.Interop.Outlook.AddressLists als = olNS.AddressLists;
if (als == null) return;
Stopwatch watcher = new Stopwatch();
foreach (Microsoft.Office.Interop.Outlook.AddressList addrList in als)
{
    if (addrList.Name == "Global Contact Address" || addrList.Name == "Global Address List")
    {
        foreach (Microsoft.Office.Interop.Outlook.AddressEntry entry in addrList.AddressEntries)
        {
            if (entry == null) continue;
            if (entry.Name == null || entry.Name.Trim() == "") continue;
            if (entry.Address == null || entry.Address.Trim() == "") continue;

            eMailInfo info = new eMailInfo();
            info.Name = entry.Name;
            MailMessage msg = new MailMessage();
            watcher.Start();
            Microsoft.Office.Interop.Outlook.ExchangeUser user = entry.GetExchangeUser();
            Debug.WriteLine(string.Format("This get exchange user time {0}", watcher.ElapsedMilliseconds));
            watcher.Reset();
            if (user != null)
            {
                watcher.Start();
                info.Address = user.PrimarySmtpAddress;
                Debug.WriteLine(string.Format("This get exchange user address time {0}", watcher.ElapsedMilliseconds));
                watcher.Reset();
            }
            else
                info.Address = entry.Address;
        }
    }
}


Solution 1:[1]

Check if these helps

Solution 2:[2]

You can try to retrieve PR_SMTP_ADDRESS using Extended MAPI (C++ or Delphi only) from multiple GAL entries in a single call - use AddressEntires.MAPITable property to retrieve the IMAPITable MAPI interface. You can then use IMAPITable::SetColumns / QueryRows or HrQueryAllRows to retrieve the PR_SMTP_ADDRESS property from multiple entries in a single call.

If Extended MAPI in C++/Delphi is not an option, you can try Redemption (I am its author) and its MAPITable object (ExecSQL method). It retrieves all SMTP addresses in a single call without opening opening each and every GAL object, so it will be at least an order of magnitude faster, maybe two order faster.

Redemption.MAPITable table = new Redemption.MAPITable();
table.Item = addrList.AddressEntries
ADODB.Recordset recordset = table.ExecSQL("SELECT \"http://schemas.microsoft.com/mapi/proptag/0x39FE001F\" from list")
while (!recordset.EOF)
{
  Debug.WriteLine(recordset.Fields[0].Value)
  recordset.MoveNext
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2