问题
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;
}
}
}
回答1:
Check if these helps
Account Object(Outlook) Look for
Outlook.OlAccountType.olExchange
in the example provided.Obtain Information for Multiple Accounts
回答2:
You can attempt to retrieve PR_SMTP_ADDRESS
using Extended MAPI (C++ or Delphi only) - 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 and its MAPITable object (ExecSQL
method):
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
}
来源:https://stackoverflow.com/questions/37590083/how-to-get-smtp-address-for-exchange-user-quickly