Creating a Distribution List Using CDOEXM and ADSI

Creating a Distribution List Using CDOEXM and ADSI

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Visual Basic

' Creating a Distribution List Using CDOEXM and ADSI
' This sample creates a distribution list in a given Active Directory container. It uses a slightly 
' modified version of the Microsoft Platform SDK code to create a group, and then 
' mail-enables the group using CDOEXM.

' A distribution list in Exchange 2000 is a normal Windows 2000 group, either security 
' or non-security, which has been mail-enabled. This sample uses the function 
' CreateDL to create the distribution list and CDOEXM to mail-enable it.

Function CreateDL(strPath As String, strGroupName As String, strSamAcctName As String, strGroupType As String, bSecurity As Boolean) As IADsGroup
' =========================================================
' CreateDL(strPath As String, strGroupName As String, strSamAcctName As String,
' strGroupType As String, bSecurity As Boolean)
'   strPath = ADsPath of the container under which the group will be created
'   strGroupName = Name of the group to be created
'   strSamAcctName = Group name as it will appear to pre-Windows 2000 computers
'   strGroupType = Type of group to be created (Local, Domain Local, Global, or Universal)
'   bSecurity = Is this a security group?

'  Function returns an IADsGroup object.

'  Example of calling the function:
'  Set objNewGroup = CreateDL("LDAP://cn=Users,dc=corp,dc=com", "My New Group", "NewGroup", "Global", False)
'  Then add users
'  objNewGroup.Add strUser1ADsPath
'  objNewGroup.Add strUser2ADsPath
' =========================================================

 Dim lGroupType As Long
 Dim iAdCont As IADsContainer
 Dim iAdGroup As IADsGroup
 Dim iMailRecip As IMailRecipient

 '  --- Set the Type of Group ------
 lGroupType = 0
 Select Case UCase(strGroupType)
 Case "LOCAL": lGroupType = ADS_GROUP_TYPE_LOCAL_GROUP
 Case "DOMAIN LOCAL": lGroupType = ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP
 Case "GLOBAL": lGroupType = ADS_GROUP_TYPE_GLOBAL_GROUP
 Case "UNIVERSAL": lGroupType = ADS_GROUP_TYPE_UNIVERSAL_GROUP
 Case Else
    Debug.Print "This is not a proper group type"
    Exit Function
 End Select


 ' ----  Check to See if it is a Security Group ----
 If bSecurity Then
   lGroupType = lGroupType Or ADS_GROUP_TYPE_SECURITY_ENABLED
 End If

 ' ---- Create the Group ----
 Set iAdCont = GetObject(strPath)
 Set iAdGroup = iAdCont.Create("group", "cn=" + strGroupName)
 iAdGroup.Put "sAMAccountName", strSamAcctName
 iAdGroup.Put "groupType", lGroupType

 ' Flush to the directory
 iAdGroup.SetInfo         

 '--- Mail Enable ----
 Set iMailRecip = iAdGroup
 iMailRecip.MailEnable

' Write Exchange information to the directory.
 iAdGroup.SetInfo

 ' Return the group.
 Set CreateDL = iAdGroup

 ' Clean up.
 Set iMailRecip = Nothing
 Set iAdGroup = Nothing
 Set iAdCont = Nothing
End Function

Send us your feedback about the Microsoft Exchange Server 2003 SDK.

Build: June 2007 (2007.618.1)

© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.