Solved

Create an Exchange Email Account Programmatically Using VB

Posted on 2001-06-12
9
1,352 Views
Last Modified: 2007-11-27
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
0
Comment
Question by:ee_guest
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 2

Expert Comment

by:Microsoft
Comment Utility
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
0
 
LVL 2

Expert Comment

by:Microsoft
Comment Utility
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
0
 
LVL 33

Accepted Solution

by:
hongjun earned 100 total points
Comment Utility
0
 
LVL 5

Expert Comment

by:AndrewDev
Comment Utility
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 5

Expert Comment

by:gbaren
Comment Utility
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.

0
 

Author Comment

by:ee_guest
Comment Utility
hongjun, your code example seems to be in favour to C.

gbaren, can you elaborate?
0
 
LVL 5

Expert Comment

by:gbaren
Comment Utility
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 = "a9d1ca15768811d1aded00c04fd8d5cd"

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.DeleteMailbox     ' 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(strConfigurationNC 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=msExchPrivateMDB);name,adspath;subtree"

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("ADsPath").Value)
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("defaultNamingContext")
varConfigNC = objRootDSE.Get("configurationNamingContext")
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_NAME_TYPE_CANONICAL_EX)

'--- 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("distinguishedName"))

' 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(varConfigNC))
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.
0
 

Expert Comment

by:CMcLennan
Comment Utility
0
 

Author Comment

by:ee_guest
Comment Utility
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
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now