获取 Exchange 通讯组列表的成员
此代码示例提示用户从“选择姓名”对话框中选择 Exchange 通讯组列表,然后展开选定通讯组列表以显示其中成员。
示例
该代码示例调用 ExchangeDistributionList 对象的 GetExchangeDistributionListMembers 方法以获取包含列表中所有成员的 AddressEntries 集合。 因为通讯组列表可以嵌套在另一个通讯组列表中,所以返回的 AddressEntries 集合可以代表任何类型的 Exchange AddressEntry 对象。
注意
[!注释] 展开通讯组列表会增加 Exchange 服务器的运行负担,因此请慎用 GetExchangeDistributionListMembers 方法。 当您的代码展开大量通讯组列表时,代码的运行速度将变慢。
用户必须连接到 Exchange 服务器并处于联机状态,才能获取通讯组列表的成员。
如果使用 Visual Studio 测试此代码示例,必须先添加对 Microsoft Outlook 15.0 对象库组件的引用,并在导入 Microsoft.Office.Interop.Outlook 命名空间时指定 Outlook 变量。 不得将 Imports 或 using 语句直接添加到此代码示例中的函数前面,这两个语句必须后跟公共类声明。 下面的代码行演示了如何在 Visual Basic 和 C# 中执行导入和分配。
Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
Private Sub GetDistributionListMembers()
Dim snd As Outlook.SelectNamesDialog = _
Application.Session.GetSelectNamesDialog()
Dim addrLists As Outlook.AddressLists = _
Application.Session.AddressLists
For Each addrList As Outlook.AddressList In addrLists
If addrList.Name = "All Groups" Then
snd.InitialAddressList = addrList
Exit For
End If
Next
snd.NumberOfRecipientSelectors = _
Outlook.OlRecipientSelectors.olShowTo
snd.ToLabel = "D/L"
snd.ShowOnlyInitialAddressList = True
snd.AllowMultipleSelection = False
snd.Display()
If (snd.Recipients.Count > 0) Then
Dim addrEntry As Outlook.AddressEntry = _
snd.Recipients(1).AddressEntry
If (addrEntry.AddressEntryUserType = _
Outlook.OlAddressEntryUserType. _
olExchangeDistributionListAddressEntry) Then
Dim exchDL As Outlook.ExchangeDistributionList = _
addrEntry.GetExchangeDistributionList()
Dim addrEntries As Outlook.AddressEntries = _
exchDL.GetExchangeDistributionListMembers()
If Not (addrEntries Is Nothing) Then
For Each exchDLMember As _
Outlook.AddressEntry In addrEntries
Debug.WriteLine(exchDLMember.Name)
Next
End If
End If
End If
End Sub
private void GetDistributionListMembers()
{
Outlook.SelectNamesDialog snd =
Application.Session.GetSelectNamesDialog();
Outlook.AddressLists addrLists =
Application.Session.AddressLists;
foreach (Outlook.AddressList addrList in addrLists)
{
if (addrList.Name == "All Groups")
{
snd.InitialAddressList = addrList;
break;
}
}
snd.NumberOfRecipientSelectors =
Outlook.OlRecipientSelectors.olShowTo;
snd.ToLabel = "D/L";
snd.ShowOnlyInitialAddressList = true;
snd.AllowMultipleSelection = false;
snd.Display();
if (snd.Recipients.Count > 0)
{
Outlook.AddressEntry addrEntry =
snd.Recipients[1].AddressEntry;
if (addrEntry.AddressEntryUserType ==
Outlook.OlAddressEntryUserType.
olExchangeDistributionListAddressEntry)
{
Outlook.ExchangeDistributionList exchDL =
addrEntry.GetExchangeDistributionList();
Outlook.AddressEntries addrEntries =
exchDL.GetExchangeDistributionListMembers();
if (addrEntries != null)
foreach (Outlook.AddressEntry exchDLMember
in addrEntries)
{
Debug.WriteLine(exchDLMember.Name);
}
}
}
}