Solved

Create an Exchange Email Account Programmatically Using VB

Posted on 2001-06-12
9
1,359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 2

Expert Comment

by:Microsoft
ID: 6184416
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
ID: 6184432
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
ID: 6184650
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:AndrewDev
ID: 6191323
0
 
LVL 5

Expert Comment

by:gbaren
ID: 6191358
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
ID: 6191378
hongjun, your code example seems to be in favour to C.

gbaren, can you elaborate?
0
 
LVL 5

Expert Comment

by:gbaren
ID: 6191553
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
 

Author Comment

by:ee_guest
ID: 6206671
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

717 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