ee_guest
asked on
Create an Exchange Email Account Programmatically Using VB
How do I create an Exchange Email Account programmatically using VB? I need this quite urgent. This means I would need to input username, password, etc.
Thanks in Advance
Thanks in Advance
go here my friend this will do exactly what you require.
http://www.planetsourcecode.com/xq/ASP/txtCodeId.4739/lngWId.1/qx/vb/scripts/ShowCode.htm
cheers
Andy
http://www.planetsourcecode.com/xq/ASP/txtCodeId.4739/lngWId.1/qx/vb/scripts/ShowCode.htm
cheers
Andy
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In general, what you need to work with is a set of objects called CDO (Collaboration Data Objects) that are installed on the computer with Outlook. If you are writing ASP code, you'll need to install Outlook on the IIS server.
ASKER
hongjun, your code example seems to be in favour to C.
gbaren, can you elaborate?
gbaren, can you elaborate?
Actually, to be technically correct, you need to use the CDOEXM (CDO for Exchange Management) or EMO (Exchange Management Objects).
The following URL will take you to sample code for many tasks using CDOEXM.:
http://msdn.microsoft.com/library/default.asp?URL=/library/psdk/exchsv2k/_mgmt_example_managing.htm
Here is the sample that you'll probably find most useful:
Creating/Deleting an Exchange Mailbox Using EMO with ADSI
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Visual Basic
'Creating/Deleting an Exchange mailbox using EMO with ADSI
'This sample uses the Exchange Management objects and ADSI to create
'a user in the Active Directory and then create an Exchange mailbox
'for that user.
'
'This sample requires a reference to the following libraries:
' - Active DS Type Library (activeds.tlb)
' - Microsoft CDO for Exchange Management Library (cdoexm.dll)
' - Microsoft ActiveX Data Objects 2.5 Library (msado15.dll)
'Included is an optional function to delete the mailbox and the user
Const GUID_USERS_CONTAINER = "a9d1ca15768811d1aded00c04 fd8d5cd"
Sub DeleteMailbox(strADspath As String, bDeleteUserAccount As Boolean)
'************************* ********** ********** **********
'This function accepts the ADsPath of a user object and uses that path to bind to the
'object in the directory and remove its mailbox. Additionally, it will remove the user
'from the directory as well
'************************* ********** ********** **********
Dim objUser As IADsUser
Dim objMailboxToDelete As IMailboxStore
Set objUser = GetObject(strADspath) ' Get the User in the directory
Set objMailboxToDelete = objUser ' Get the IMailboxStore interface
objMailboxToDelete.DeleteM ailbox ' Delete the mailbox
objUser.SetInfo
If bDeleteUserAccount Then
Dim objContainer As IADsContainer
Set objContainer = GetObject(objUser.Parent)
objContainer.Delete objUser.Class, objUser.Name 'Delete user object
Set objContainer = Nothing
End If
' Clean Up
Set objMailboxToDelete = Nothing
Set objUser = Nothing
End Sub
Function FindAnyMDB(strConfiguratio nNC As String) As String
'************************* ********** ********** **********
' This function takes the configuration naming context as a parameter
' and will return the ADsPath for the first private information store
' that it finds.
'************************* ********** ********** **********
Dim oConnection As New ADODB.Connection
Dim oCommand As New ADODB.Command
Dim oRecordSet As ADODB.Recordset
Dim strQuery As String
' Open the Connection
oConnection.Provider = "ADsDSOObject"
oConnection.Open "ADs Provider"
' Build the query to find the private MDB
strQuery = "<LDAP://" & strConfigurationNC & ">;(objectCategory=msExchP rivateMDB) ;name,adsp ath;subtre e"
oCommand.ActiveConnection = oConnection
oCommand.CommandText = strQuery
Set oRecordSet = oCommand.Execute
' If we have an MDB then return the first one
If Not oRecordSet.EOF Then
oRecordSet.MoveFirst
FindAnyMDB = CStr(oRecordSet.Fields("AD sPath").Va lue)
Else
FindAnyMDB = ""
End If
'Clean Up
oRecordSet.Close
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
End Function
Sub Main()
Dim objGUIDContainer As IADs
Dim objUsersContainer As IADsContainer
Dim objRootDSE As IADs
Dim objDomain As IADs
Dim objUser As IADsUser
Dim objMailbox As IMailboxStore
Dim objNameTranslate As New NameTranslate
Dim varDomainNC As Variant
Dim varConfigNC As Variant
Dim varDnsDomain As Variant
Dim strUserName As String
Dim strSamAccountName As String
Dim strPassword As String
Dim strExchangeOrg As String
Dim strAdminGroup As String
Dim strHomeMdbADsPath As String
'--- Set Variables ---
strUserName = "User13"
strSamAccountName = "User13"
strPassword = "password"
'See additional samples and documentation for help determining your organization and admin groups
strExchangeOrg = "Microsoft"
strAdminGroup = "First Administrative Group"
' Get the current domain
Set objRootDSE = GetObject("LDAP://RootDSE" )
varDomainNC = objRootDSE.Get("defaultNam ingContext ")
varConfigNC = objRootDSE.Get("configurat ionNamingC ontext")
Set objDomain = GetObject("LDAP://" + varDomainNC)
' Get the DNS name for the domain using IADsNameTranslate for the UPN
objNameTranslate.Init ADS_NAME_INITTYPE_DOMAIN, objDomain.Get("name")
objNameTranslate.Set ADS_NAME_TYPE_1779, varDomainNC
varDnsDomain = objNameTranslate.Get(ADS_N AME_TYPE_C ANONICAL_E X)
'--- Get the Users Container for this domain using the well-known GUID---
Set objGUIDContainer = GetObject("LDAP://<WKGUID= " + GUID_USERS_CONTAINER + "," + varDomainNC + ">")
' Get a container interface for that object in the directory
Set objUsersContainer = GetObject("LDAP://" & objGUIDContainer.Get("dist inguishedN ame"))
' Create the user object in cache
Set objUser = objUsersContainer.Create(" User", "CN=" + strUserName)
objUser.Put "samAccountName", strSamAccountName
objUser.Put "displayName", strUserName
objUser.Put "userPrincipalName", strUserName + "@" + varDnsDomain
'Flush the user object to the directory
objUser.SetInfo
'Set the user's password
objUser.SetPassword strPassword
'Build the ADsPath for the HomeMDB of the mailbox
strHomeMdbADsPath = FindAnyMDB(CStr(varConfigN C))
If strHomeMdbADsPath <> "" Then
Set objMailbox = objUser 'QI for IMailboxStore
objMailbox.CreateMailbox strHomeMdbADsPath
objUser.Put "legacyExchangeDN", "/o=" + strExchangeOrg + "/ou=" + strAdminGroup + "/cn=Recipients/cn=" + strUserName
objUser.AccountDisabled = False
objUser.SetInfo
End If
'To delete the account and mailbox after they have been created uncomment the following line
'DeleteMailbox objUser.ADsPath, True
Set objUser = Nothing
Set objMailbox = Nothing
Set objGUIDContainer = Nothing
Set objUsersContainer = Nothing
Set objDomain = Nothing
Set objNameTranslate = Nothing
End Sub
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
? 2000 Microsoft Corporation. All rights reserved. Terms of use.
The following URL will take you to sample code for many tasks using CDOEXM.:
http://msdn.microsoft.com/library/default.asp?URL=/library/psdk/exchsv2k/_mgmt_example_managing.htm
Here is the sample that you'll probably find most useful:
Creating/Deleting an Exchange Mailbox Using EMO with ADSI
--------------------------
Visual Basic
'Creating/Deleting an Exchange mailbox using EMO with ADSI
'This sample uses the Exchange Management objects and ADSI to create
'a user in the Active Directory and then create an Exchange mailbox
'for that user.
'
'This sample requires a reference to the following libraries:
' - Active DS Type Library (activeds.tlb)
' - Microsoft CDO for Exchange Management Library (cdoexm.dll)
' - Microsoft ActiveX Data Objects 2.5 Library (msado15.dll)
'Included is an optional function to delete the mailbox and the user
Const GUID_USERS_CONTAINER = "a9d1ca15768811d1aded00c04
Sub DeleteMailbox(strADspath As String, bDeleteUserAccount As Boolean)
'*************************
'This function accepts the ADsPath of a user object and uses that path to bind to the
'object in the directory and remove its mailbox. Additionally, it will remove the user
'from the directory as well
'*************************
Dim objUser As IADsUser
Dim objMailboxToDelete As IMailboxStore
Set objUser = GetObject(strADspath) ' Get the User in the directory
Set objMailboxToDelete = objUser ' Get the IMailboxStore interface
objMailboxToDelete.DeleteM
objUser.SetInfo
If bDeleteUserAccount Then
Dim objContainer As IADsContainer
Set objContainer = GetObject(objUser.Parent)
objContainer.Delete objUser.Class, objUser.Name 'Delete user object
Set objContainer = Nothing
End If
' Clean Up
Set objMailboxToDelete = Nothing
Set objUser = Nothing
End Sub
Function FindAnyMDB(strConfiguratio
'*************************
' This function takes the configuration naming context as a parameter
' and will return the ADsPath for the first private information store
' that it finds.
'*************************
Dim oConnection As New ADODB.Connection
Dim oCommand As New ADODB.Command
Dim oRecordSet As ADODB.Recordset
Dim strQuery As String
' Open the Connection
oConnection.Provider = "ADsDSOObject"
oConnection.Open "ADs Provider"
' Build the query to find the private MDB
strQuery = "<LDAP://" & strConfigurationNC & ">;(objectCategory=msExchP
oCommand.ActiveConnection = oConnection
oCommand.CommandText = strQuery
Set oRecordSet = oCommand.Execute
' If we have an MDB then return the first one
If Not oRecordSet.EOF Then
oRecordSet.MoveFirst
FindAnyMDB = CStr(oRecordSet.Fields("AD
Else
FindAnyMDB = ""
End If
'Clean Up
oRecordSet.Close
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
End Function
Sub Main()
Dim objGUIDContainer As IADs
Dim objUsersContainer As IADsContainer
Dim objRootDSE As IADs
Dim objDomain As IADs
Dim objUser As IADsUser
Dim objMailbox As IMailboxStore
Dim objNameTranslate As New NameTranslate
Dim varDomainNC As Variant
Dim varConfigNC As Variant
Dim varDnsDomain As Variant
Dim strUserName As String
Dim strSamAccountName As String
Dim strPassword As String
Dim strExchangeOrg As String
Dim strAdminGroup As String
Dim strHomeMdbADsPath As String
'--- Set Variables ---
strUserName = "User13"
strSamAccountName = "User13"
strPassword = "password"
'See additional samples and documentation for help determining your organization and admin groups
strExchangeOrg = "Microsoft"
strAdminGroup = "First Administrative Group"
' Get the current domain
Set objRootDSE = GetObject("LDAP://RootDSE"
varDomainNC = objRootDSE.Get("defaultNam
varConfigNC = objRootDSE.Get("configurat
Set objDomain = GetObject("LDAP://" + varDomainNC)
' Get the DNS name for the domain using IADsNameTranslate for the UPN
objNameTranslate.Init ADS_NAME_INITTYPE_DOMAIN, objDomain.Get("name")
objNameTranslate.Set ADS_NAME_TYPE_1779, varDomainNC
varDnsDomain = objNameTranslate.Get(ADS_N
'--- Get the Users Container for this domain using the well-known GUID---
Set objGUIDContainer = GetObject("LDAP://<WKGUID=
' Get a container interface for that object in the directory
Set objUsersContainer = GetObject("LDAP://" & objGUIDContainer.Get("dist
' Create the user object in cache
Set objUser = objUsersContainer.Create("
objUser.Put "samAccountName", strSamAccountName
objUser.Put "displayName", strUserName
objUser.Put "userPrincipalName", strUserName + "@" + varDnsDomain
'Flush the user object to the directory
objUser.SetInfo
'Set the user's password
objUser.SetPassword strPassword
'Build the ADsPath for the HomeMDB of the mailbox
strHomeMdbADsPath = FindAnyMDB(CStr(varConfigN
If strHomeMdbADsPath <> "" Then
Set objMailbox = objUser 'QI for IMailboxStore
objMailbox.CreateMailbox strHomeMdbADsPath
objUser.Put "legacyExchangeDN", "/o=" + strExchangeOrg + "/ou=" + strAdminGroup + "/cn=Recipients/cn=" + strUserName
objUser.AccountDisabled = False
objUser.SetInfo
End If
'To delete the account and mailbox after they have been created uncomment the following line
'DeleteMailbox objUser.ADsPath, True
Set objUser = Nothing
Set objMailbox = Nothing
Set objGUIDContainer = Nothing
Set objUsersContainer = Nothing
Set objDomain = Nothing
Set objNameTranslate = Nothing
End Sub
--------------------------
? 2000 Microsoft Corporation. All rights reserved. Terms of use.
ASKER
gbaren, I have posted another question for you.
Title is "points for gbaren".
Please go there and post a comment or answer. It is for you. Thanks a lot to everyone
Title is "points for gbaren".
Please go there and post a comment or answer. It is for you. Thanks a lot to everyone
http://www.planetsourcecode.com/xq/ASP/txtCodeId.4739/lngWId.1/qx/vb/scripts/ShowCode.htm
cheers
Andy