A distribution list can be created by accessing the Active directory. The object distinguished name of a distribution list will typically look like
A Distribution list needs two very basic attributes to be specified at the minimum. One is display name and the second is the alias. A distribution list or a Mailbox can be created only under the Recipients container.
To create a distribution list under the Recipients container,
- Get the ADSI DirectoryEntry for the Recipients container.
- Create a distribution list by specifying "groupOfNames".
- Specify the other properties like displaynames, smtp, x400, owner etc.,
- Ensure that the Security Descriptor is properly set for the owner attribute.
- Commit the changes to the AD.
The class DirectoryEntry.Children.Add("distributionlistname","groupOfNames") can be used for creating a Distribution List under the recipients container. A call to this function returns an object for the DirectoryEntry for the newly created Distribution List. This does not actually get updated until we call the DirectoryEntry.commitchanges method.
After getting a pointer to the Distribution list as an object of DirectoryEntry, the following properties are to be updated before calling CommitChanges.
This is denoted by the cn property name in Exchange. This can be updated by calling the DirectoryEntry.Properties["cn"]. Do call this once before committing the Changes. This ensures that the Display name is updated properly.
This is denoted by the uid propertyin Exchange Server. This is the alias name for the distribution list and will typically be used for creating the Object Distinguished name under the container.
Email Addresses Property:
There could be many types of email addresses in an Exchange environment. They may be smtp, x400, ccmail etc., Set the relevant email addresses. If it is smtp, then mail attribute should be set and for X400 it is textencodedaddress.
While discussing about the email addresses one point is worth mentioning. The Exchange server keeps all the Email addressing data under a node at SiteAddressing. This data can be pulled from an attribute called GateWay-Proxy, which is not available via ADSI. But it can be pulled using ADO.
Members of Distribution List:
This is managed under the member property of Exchange. This is an array of list of mailboxes or distribution lists.
This property is stored under the owner attribute of the Distribution List. There will be a problem when we create the DL and assign an owner. As the DL is freshly created, it won't have any SecurityDescriptor associated with it. To solve this problem, we can pull the SecurityDescriptor of the owner and assign it to the DL.
The Properties of Exchange 5.5 do not understand the LDAP naming with slashes. It returns an error saying Protocol Error. In such cases, use the comma formatted LDAP Object Distinguished Names. I am not sure about the later versions of Exchange. Be sure to check this out.
Also some of the properties while setting may return an error saying that the property is not found. In those cases, use the DirectoryEntry.Properties.Add function to add the property. The reason is because, the exchange directory may not keep the property fields if it is not a Required field. In such cases, adding the property will help.
The attached sample includes two parts. One is the set of classes which can be reused as such for any C# DL Creation programs or may be extended. The second is the DL Creation functional part. This extensively uses CDO libraries for finding the necessary Server names, Site Names and the much needed Object Distinguished names for many entities.
The attached sample project can be downloaded here.
There are two classes available in the DLManagement namespaces which can be used in other C# programs. One class called DLManager, holds the utility functions which could be used for common operations required for creation or modifications of distribution list. The other class DistributionList, holds the data regarding the Distribution List.
These reusable classes can be downloaded here.
Some of the functions in this DLManager are described as below. All the functions in this class are static public members. So they can be used without instantiating this class.
- GetSites - This function can return a list of sites under an Exchange organization.
- GetOrganization - This function returns the Organization name for a CDO Address entry object
- IsLDAPEntryWithSlashes - Some properties of Exchange 5.5 need the entries with comma separated names in Object Distinguished Names. This function can be used to check if the entry contains slashes.
- GetHomeServer - This can be used to find the Home server for a particular distribution list.
- UpdateDL - This function can be used for either creating or modifying a DL. This takes care of setting all the above said properties, including adding the owner of the DL with its Security Descriptor.
- FindDL - This function can find and populate the details of a DL and return an object of DistributionList class.
- FindSMTPAddressing - This function gets the SMTP Addressing format for a site using ADO.
This class contains only data members to hold the properties of DLs. This can be used either way to retrieve or update the Distribution Lists using the DLManager class.
This application was written with Visual Studio .Net 2003. The Libraries used are as follows:
- Microsoft CDO 1.21 Libraries. This is available by default in your system if Outlook 2000 or above is installed.
- ADODB. This can be included from References --> Com --> Microsoft ActiveX Data Objects 2.6 & above.