In this section, you will begin an exercise that will yield the creation of the MSExchAdmin.DLL COM server.
Create a new ActiveX DLL Visual Basic project.
Set a reference to the Active DS Type Library by clicking the Project menu, selecting References , and placing a checkmark next to the "Active DS Type Library" entry. Click the OK command button to exit the References “Project1 dialog box.
Rename Project1 as MSExchAdmin .
Rename the Class1 class module as ExchangeObjectMgt .
Enter the following code into the General Declarations section of the class module:
Public Function CreateExchangeMailbox(ByVal ExchangeServerName As String, ByVal ExchangeServerOrganization As String, ByVal ExchangeServerSite As String, ByVal MailboxRelativePath As String, ByVal MailboxDisplayName As String, ByVal MailboxFirstName As String, ByVal MailboxLastName As String, ByVal MailboxMiddleInitial As String, ByVal MailboxAlias As String, ByVal UserDomain As String, ByVal NTUserAccountToAssociate As String) As Boolean Dim Mailbox As IADs Dim MailboxParentContainer As IADs Dim MTA As String Dim MDB As String Dim MailboxSMTPAddress As String Dim MailboxX400Address As String Dim MailboxCCMailAddress As String Dim MailboxMSMailAddress As String Dim SID As New ADsSID Dim Security As New ADsSecurity Dim SecurityDescriptor As IADsSecurityDescriptor Dim DiscretionaryACL As IADsAccessControlList Dim AccessControlEntry As New AccessControlEntry MTA = "cn=Microsoft MTA,cn="&ExchangeServerName& ",cn=Servers,cn=Configuration,ou="&ExchangeServerSite&",o="& ExchangeServerOrganization MDB = "cn=Microsoft Private MDB,cn="&ExchangeServerName& ",cn=Servers,cn=Configuration,ou="&ExchangeServerSite&",o="& ExchangeServerOrganization MailboxSMTPAddress = MailboxAlias&"@"&ExchangeServerSite&"."& ExchangeServerOrganization&".com" MailboxX400Address = "c=US;a= ;p="&ExchangeServerOrganization&";o="& ExchangeServerSite&";s="&MailboxLastName&";g="&MailboxFirstName&";i="& MailboxMiddleInitial MailboxCCMailAddress = MailboxLastName&", "&MailboxFirstName&" at "& ExchangeServerSite MailboxMSMailAddress = UCase(ExchangeServerOrganization&"/"& ExchangeServerSite&"/"&MailboxAlias) Set MailboxParentContainer = GetObject("LDAP://"&ExchangeServerName&"/o=" MailboxRelativePath) Set Mailbox = MailboxParentContainer.Create("organizationalPerson", "cn="& MailboxAlias) Mailbox.Put "mailPreferenceOption", 0 Mailbox.Put "givenName", MailboxFirstName Mailbox.Put "sn", MailboxLastName Mailbox.Put "cn", MailboxDisplayName Mailbox.Put "uid", MailboxAlias Mailbox.Put "Home-MTA", MTA Mailbox.Put "Home-MDB", MDB Mailbox.Put "mail", MailboxSMTPAddress Mailbox.Put "MAPI-Recipient", True Mailbox.Put "rfc822Mailbox", MailboxSMTPAddress Mailbox.Put "textEncodedORAddress", MailboxX400Address Mailbox.PutEx ADS_PROPERTY_APPEND, "otherMailbox", Array("CCMAIL$"& MailboxCCMailAddress, "MS$"&MailboxMSMailAddress) SID.SetAs ADS_SID_WINNT_PATH, "WinNT://"&UserDomain&"/"& NTUserAccountToAssociate&",user" sidHex = SID.GetAs(ADS_SID_HEXSTRING) Mailbox.Put "Assoc-NT-Account", sidHex Mailbox.SetInfo Set SecurityDescriptor = Security.GetSecurityDescriptor(Mailbox.ADsPath) Set DiscretionaryACL = SecurityDescriptor.DiscretionaryACL AccessControlEntry.AceType = ADS_ACETYPE_ACCESS_ALLOWED AccessControlEntry.Trustee = UserDomain&"\"&MailboxAlias AccessControlEntry.AccessMask = ADS_RIGHT_EXCH_MAIL_SEND_AS Or ADS_RIGHT_EXCH_MAIL_RECEIVE_AS Or ADS_RIGHT_EXCH_MODIFY_USER_ATT DiscretionaryACL.AddAce AccessControlEntry SecurityDescriptor.DiscretionaryACL = DiscretionaryACL Security.SetSecurityDescriptor SecurityDescriptor If Err.Number = 0 Then CreateExchangeMailbox = True End Function Public Function RemoveExchangeMailbox(ByVal ExchangeServerName As String, ByVal ExchangeServerOrganization As String, ByVal ExchangeServerSite As String, ByVal MailboxRelativePath As String, ByVal MailboxToRemove As String) As Boolean Dim MailboxParentContainer As IADsContainer Set MailboxParentContainer = GetObject("LDAP://"&ExchangeServerName&"/o=" & ExchangeServerOrganization&"/ou="&ExchangeServerSite&"/cn=Recipients"& MailboxRelativePath) Call MailboxParentContainer.Delete("organizationalPerson", "cn="& MailboxToRemove) If Err.Number = 0 Then RemoveExchangeMailbox = True End Function Public Function AddExchangeDLMember(ByVal ExchangeServerName As String, ByVal ExchangeServerOrganization As String, ByVal ExchangeServerSite As String, ByVal DistributionListRelativePath As String, ByVal DistributionListName As String, ByVal DLMemberMailboxName As String) As Boolean Dim DistributionList As IADs Set DistributionList = GetObject("LDAP://"&ExchangeServerName&"/o="& ExchangeServerOrganization&"/ou="&ExchangeServerSite&"/cn=Recipients/cn="& DistributionListName) DistributionList.Add ("LDAP://"&ExchangeServerName&"/o="& ExchangeServerOrganization&"/ou="&ExchangeServerSite&"/cn=Recipients"& "/cn="&DLMemberMailboxName) DistributionList.SetInfo If Err.Number = 0 Then AddExchangeDLMember = True End Function Public Function RemoveExchangeDLMember(ByVal ExchangeServerName As String, ByVal ExchangeServerOrganization As String, ByVal ExchangeServerSite As String, ByVal DistributionListRelativePath As String, ByVal DistributionListName As String, ByVal DLMemberMailboxName As String) As Boolean Dim DistributionList As IADs Set DistributionList = GetObject("LDAP://"&ExchangeServerName&"/o="& ExchangeServerOrganization&"/ou="&ExchangeServerSite&"/cn=Recipients"& DistributionListRelativePath&"/cn="&DistributionListName) DistributionList.Remove ("LDAP://"&ExchangeServerName&"/o="& ExchangeServerOrganization&"/ou="&ExchangeServerSite& "/cn=Recipients"&"/cn="&DLMemberMailboxName) DistributionList.SetInfo If Err.Number = 0 Then RemoveExchangeDLMember = True End Function Public Function DetermineExchangeDLOwner(ByVal ExchangeServerName As String, ByVal ExchangeServerOrganization As String, ByVal ExchangeServerSite As String, ByVal DistributionListRelativePath As String, ByVal DistributionListName As String) As String Dim DistributionList As IADs Set DistributionList = GetObject("LDAP://"&ExchangeServerName&"/o="& ExchangeServerOrganization&"/ou="&ExchangeServerSite&"/cn=Recipients"& DistributionListRelativePath&"/cn="&DistributionListName) DetermineExchangeDLOwner = DistributionList.Owner End Function
Compile the code as MSExchAdmin.DLL.
Save and close the MSExchAdmin project.
Tip
If you do not want to share your code between applications, you can enter the preceding code into a code module in any Visual Basic application .
Tip
You can download the Visual Basic 6.0 project or precompiled version of MSExchAdmin.DLL from http://www.newriders.com/adsi.
With the ExchangeObjectMgt class module created, you can access the functions contained in the class module from any programming language that supports OLE automation, including Visual Basic, VBScript, and JavaScript.
Tip
To instantiate the object, follow the appropriate syntax found in Chapter 3. Substitute the ExchangeObjectMgt class name where necessary .
Use the Table 11.2 to help you use the proper syntax for each of the methods of the ExchangeObjectMgt interface.
Action | Syntax |
---|---|
Create a New Exchange Mailbox | Debug.Print CreateExchangeMailbox ("Exchange_Server", "ADSITest", "Macmillan", "","Milan Kundera", "Milan","Kundera","-", "Milan.Kundera","UserDomain", "mkundera") |
Remove an Existing Exchange Mailbox | Debug.Print RemoveExchangeMailbox ("Exchange_Server", "ADSITest", "Macmillan", "","Milan.Kundera") |
Add an Existing Mailbox as Member of an Existing Distribution List | Debug.Print AddExchangeDLMember ("Exchange_Server", "ADSITest", "Macmillan", "", "DL_Senior_Management", "Julian.Barnes") |
Remove Member from Distribution List | Debug.Print RemoveExchangeDLMember ("Exchange_Server", "ADSITest", "Macmillan", "", "DL_Senior_Management", "Milan.Kundera") |
Determine Distribution List Owner | Debug.Print DetermineExchangeDLOwner ("Exchange_Server", "ADSITest", "Macmillan", "", "DL_Senior_Management") |
Top |