如何快速获取Exchange用户的SMTP地址?

时间:2016-06-02 11:05:35

标签: c# email outlook

以下是我的代码,PrimarySmtpAddress使用50ms,需要超过15秒来加载更多300个用户。我不熟悉Outlook API,看起来这个属性是检索SMTP地址的常用方法,但它太慢了。

是否有其他方法可以检索SMTP地址或我错误地使用此属性?

.NetFramework是3.5

Outlook版本是2010年

Microsoft.Office.Interop.Outlook是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;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

检查这些是否有帮助

答案 1 :(得分:0)

您可以尝试使用扩展MAPI(仅限C ++或Delphi)检索PR_SMTP_ADDRESS - 使用AddressEntires.MAPITable属性检索IMAPITable MAPI接口。然后,您可以使用IMAPITable::SetColumns / QueryRowsHrQueryAllRows在一次通话中从多个条目中检索PR_SMTP_ADDRESS属性。

如果无法选择C ++ / Delphi中的扩展MAPI,则可以尝试使用Redemption及其MAPITable对象(ExecSQL方法):

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
}