Creating a Mailbox in an Exchange Server RRS feed

  • Question

  • User-1668286398 posted

    I am finally done with creating a user, enabling the user, setting the password and adding that user to the specified groups. Now all thats left is to create a mailbox for the user. I am using VB.NET, IIS 6.0 and Exchange Server 5.5. Is it possible to create a mailbox using LDAP.

    If not. Are there any other technologies using which we can create a  mailbox.

    Wednesday, March 8, 2006 6:03 AM

All replies

  • User843746731 posted

    Wow, just finished this myself! So count your lucky stars [;)]

    My function has ended up quite big, but you want something like:

    Shared Function createExchange55Mailbox(ByVal FirstName As String, ByVal lastName As String, ByVal Domain As String, ByVal Username As String, _

    ByVal exServer As String, ByVal exSite As String, ByVal exOrg As String, ByVal dptID As Int16) As String

    ' First get the SID and SD for the user, which we will need later on.
    Dim objUI As New
    Dim arrSID, arrSD As
    objUI.GetSidFromName(Domain, Username, arrSID)
       objUI.GenerateSecDescriptor(Domain, Username, arrSD)
    objUI =

    'Set the Vars to be used
    Dim strEmailAlias As String
    = Trim(Replace(Trim(FirstName), " ", "") & "." & Replace(Trim(lastName), " ", ""))

    'The path to connect to the Exchange box with
    Dim LDAPpath As String
    = exServer & "/o=" & exOrg & "/ou=" & exSite & "/cn=Recipients"

    'Set the MTA and MDB properties - CHange the o - ou and cn stuff, to your servers - This is our dev server ;)
    Dim MTA As String
    = "cn=Microsoft MTA,cn=SLONIT01,cn=Servers,cn=Configuration,ou=RestoreSite1,o=RestoreOrg"
    Dim MDB As String
    = "cn=Microsoft Private MDB,cn=SLONIT01,cn=Servers,cn=Configuration,ou=RestoreSite1,o=RestoreOrg"

    ' X400Address - Set GB to US or whatever country you are in etc...
    Dim x400address As String
    = "c=GB;a= ;p=" & exOrg & ";o=" & exSite & ";s=" & lastName & ";g=" & FirstName & ";"

    'New add the Exchaneg box via the .NET framework
    'Connect to the LDAP directory
    Dim de As New
    DirectoryEntry("LDAP://" & LDAPpath)

    'Set the mailbox object
    Dim objMB As

    'Set it to the added mailbox
    objMB = de.Children.Add("cn="& strEmailAlias, "organizationalPerson")

    'Now add the properties
    objMB.Properties("mailPreferenceOption").Value = 0
    objMB.Properties("givenName").Value = Trim(FirstName)
    objMB.Properties("sn").Value = Trim(lastName)
    objMB.Properties("cn").Value = Trim(lastName & ", " & FirstName)
    objMB.Properties("uid").Value = Trim(strEmailAlias)
    objMB.Properties("Home-MTA").Value = Trim(MTA)
    objMB.Properties("Home-MDB").Value = Trim(MDB)
    objMB.Properties("mail").Value = strEmailAlias & "@aig.com"
    objMB.Properties("MAPI-Recipient").Value =

    ' Insert the X400 and X500 address'
    objMB.Properties("textEncodedORaddress").Value = Trim(x400address)

    ' Associate the NT User account


    Return "Created the .NET mailbox!"

    End Function


    It's taken a couple of days to get this all together and remeber the IIS6 / WPG / Impersonated account will need the right to create mailboxes, the best tip is if you can turn on MAPI / LDAP logging to maximum on the Exchange box then you'll get some useful error codes.

    Also you'll need to use the AcctCrt.dll file found in the SDK, this will need to be registered on the IIS server and again the account will need to be able to query the SID's. I did pull the SID out using the System.DirectoryServices, as in this article: http://www.netomatix.com/GetUserSid.aspx but it wouldn't insert into the mailbox... Most bizarre! Both ways seem to a return a byte array.

    So if you can find a way to get the SID and Security Descriptor using System.DirectoryServices that can be inserted into the new mailbox item, I'd be most grateful! Then it wouldn't relay on any external components!!!

    What I also ended up writing (as I couldn't find something that does it already), was a function to display all the LDAP properties and their values, so you'll be able to find the property names from an existing mailbox and replicate them to create a mailbox with all the details available:

    Shared Function returnExchange55Details() As String

    Dim sReturn As String
    Dim searchRoot As New

    Dim ds As New DirectorySearcher(searchRoot, "sn=Surname")
    Dim dsc As
    SearchResultCollection = ds.FindAll()

    sr As SearchResult = dsc.Item(0)
    Dim i, z As

    Dim prop As IEnumerator
    Dim val As

    Dim FinalVal As ResultPropertyValueCollection

    prop = sr.Properties.PropertyNames.GetEnumerator
    val = sr.Properties.Values.GetEnumerator

    For i = 0 To sr.Properties.Count - 1
       FinalVal = val.Current

          sReturn = sReturn & prop.Current & "<br />"

             For z = 0 To FinalVal.Count - 1
                sReturn = sReturn & " - " & FinalVal.Item(z) & "<br />"
    sReturn = sReturn & "<hr />"

    Return sReturn

    End Function


    Please bear in mind I'm pretty new to .NET, been *cough* developing in ASP for ages, so some of my code may not be the most elegant, or well placed, so I'd also really appreciate any comments on improvements to general syntax, structuring and such like!

    Anyway, HTH! [:D]

    Wednesday, March 8, 2006 8:58 AM
  • User-1668286398 posted

    Hey I saw your code. Its quite complicated and I am not quite sure that I understood it. But I had found this particular code in some other forum. I have tested it as a VBScript and it works fine. But when modified to VB.NET. It returns this Error.

    Object not found.


    This is the entire code. Could you point out any errors that you spot.

    Dim strServer, strexchangeorg, strdomain, strAdminGroup, strStoragegroup, strstorename As String
            Dim strmailbox As String

            strServer = "HV-AUTO-SYS"
            strexchangeorg = "AUTO First Organization"
            strdomain = "DC=AUTO,DC=COM"

            strAdminGroup = "First Administrative Group"
            strStoragegroup = "First Storage Group"
            strstorename = "Mailbox Store (" & strServer & ")"

            strmailbox = strmailbox & "CN=" & strstorename
            strmailbox = strmailbox & ",CN=" & strStoragegroup
            strmailbox = strmailbox & ",CN=InformationStore"
            strmailbox = strmailbox & ",CN=" & strServer
            strmailbox = strmailbox & ",CN=Servers"
            strmailbox = strmailbox & ",CN=" & strAdminGroup
            strmailbox = strmailbox & ",CN=Administrative Groups"
            strmailbox = strmailbox & ",CN=" & strexchangeorg
            strmailbox = strmailbox & ",CN=Microsoft Exchange"
            strmailbox = strmailbox & ",CN=Services"
            strmailbox = strmailbox & ",CN=Configuration"
            strmailbox = strmailbox & "," & strdomain

            'Response.Write("Mailbox " & strmailbox)
            Dim oIADSUser As ActiveDs.IADsUser
            Dim objmailbox As CDOEXM.IMailboxStore

            oIADSUser = GetObject("LDAP://CN= tom dick xyz,OU=user,OU=location,DC=auto,DC=com")
            objmailbox = oIADSUser


    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> Cheers,</o:p>


    Friday, March 17, 2006 5:34 AM
  • User843746731 posted

    Hi there!

    Cool, glad you liked it ;)

    For this code, it's creating a Exchange 2000 / 2003 mailbox right? On an AD domain?

    As I'm working my way along with .NET and sadly at our co. we're still on Exchange 5.5 and Windows NT4 domain! Shocking I know!!!

    Anyway, object not found generally means the code isn't wrong per se, just the location of where it's looking...

    The first thing I'd check is whether CDOEXM.dll is registered on the webserver you're running the code on.

    Next I'd check the LDAP location... There's a LDAP.exe (or LDP.exe maybe) on the 2000 / 2003 server CD's which allow you to connect to the LDAP directories, so you could check if your location there is correct.

    Let me know how you get on!

    GL & HTH!

    Friday, March 17, 2006 9:52 AM
  • User-1668286398 posted

    Hello there!!!

     My compnay uses Windown Server 2003 with Exchange 6.5, the webserver is IIS. It is creating a mailbox for a user already present in the AD.

    The LDAP location is correct. I had copied it from the LDP utility itself.

    That leaves CDOEXM.dll. I have added a reference to it and imported in my code. Do I need to do anything else? How exactly can I register CDOEXM.dll to the IIS? I thought that C:/>Aspnet_regiis -s <path> would do the trick. But nothing happens. I was led to believe that these files get auto registered when the exchange server is installed.

    I really appreciate your helping me out.

    Cheers,                                                                                                                                   Saurabh

    Monday, March 20, 2006 4:08 AM
  • User843746731 posted

    Hiya, if I understand correctly, referencing the dll in VS only makes the intellisense work properly with that object...

    So from the IIS server's desktop (you can use RDC), from a cmd line:

    RegSvr32 "FullPathtoDLLFile\CDOEXM.dll"

    Give it a go and let me know [;)]

    Monday, March 20, 2006 8:08 AM
  • User-1668286398 posted

    Hello Mark,

             I registered CDOEXM component using that command. I also tried to use the tlbimp utility to import the component. But both dont work. [:(] I get the same error. The component is registered as its displayed in the Component services manager. Now what could be the cause of the error. The error is obviously generated by the COM component itself.

             As an alternative solution I am trying to directly call the VBScript and pass as argument to it. But I would prefer to create the mailbox in the program rather than call an external script. I am at a loss to understand the cause of this error.

             Thanks again for your patience,



    Wednesday, March 22, 2006 2:47 AM
  • User614611264 posted


    I have the same problem.  I can not create a mailbox by .NET.  The code is simply as follows:

    Dim user As New DirectoryEntry("LDAP://,cn=users,dc=mydomain,dc=local")

    Dim mbx As IMailboxStore = User.NativeObject

    mbx.CreateMailbox("my mailbox store's distinguished name which i copied from adsiedit tool")


    This code raises the following exception on line mbx.createmailbox:  "Parameter is incorrect"

    When I try to execute the script below (with the same distinguished name above) it works fine and cretaes the mailbox. Bu I dont prefer to run an external script.  Could you find a way to create the mailbox  using .NET?

    Dim oIADSUser
    Dim oMailbox
    Dim userAlias

    Set oIADS = GetObject("LDAP://RootDSE")
    strDefaultNC = oIADS.Get("defaultnamingcontext")
    MsgBox FindAnyMDB("CN=Configuration," & strDefaultNC)
    'TODO: Use the newly created domain user account to replace the "UserName".

    MsgBox strDefaultNC

    Dim path
    path="LDAP://CN=" & userAlias & ",CN=Users," & strDefaultNC
    MsgBox path
    Set oIADSUser = GetObject(path)

    Set oMailBox = oIADSUser
    oMailbox.CreateMailbox FindAnyMDB("CN=Configuration," & strDefaultNC)

    Function FindAnyMDB(strConfigurationNC)
     Dim oConnection
     Dim oCommand
     Dim oRecordSet
     Dim strQuery

     ' Open the Connection.
     Set oConnection = CreateObject("ADODB.Connection")
     set oCommand = CreateObject("ADODB.Command")
     Set oRecordSet = CreateObject("ADODB.Recordset")

     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 you have an MDB, return the first one.
     If Not oRecordSet.EOF Then
      FindAnyMDB = CStr(oRecordSet.Fields("ADsPath").Value)
      FindAnyMDB = ""
     End If

     'Clean up.
     Set oRecordSet = Nothing
     Set oCommand = Nothing
     Set oConnection = Nothing
    End Function

    Ayşe Çöloğlu

    Wednesday, March 22, 2006 7:32 AM
  • User843746731 posted

    Hi guys,

    I've been pondering this but can't seem to get my head round it fully...

    Hopefully we'll soon upgrade to Exchange 2003, however it'll be moving to another datacentre then so I don't know if we'll have the same kind of control over it and if they'll want us to create Mailboxes like that... Hopefully they will however [;-)]

    ANyway, the only thing I can think it could be is either permissions... Or just thinking out loud here, I had to do a:


    On a lot of the vars that were being entered into the mailbox name... Maybe try that? And do it on the var as it goes into the mailbox???

    There must be a way!!!

    Also, maybe have a look around on cdolive.com, some great resources on there, really helped me out!



    Wednesday, March 29, 2006 8:05 AM
  • User843746731 posted

    Guys, I spotted this on here:


    It may help???

    Thursday, April 6, 2006 6:48 AM
  • User-1668286398 posted

    Hey Mark!

    I solved the problem. It turned out to be a simple permissions issue. The User of the program, NETWORK SERVICE was not the member of the Exchange Administrators group. I really hate it when solutions to seemingly complicated problems are so simple. You have to go to System Manager in the Exchange Server and simply add permissions and voila it works.

    Tha link that you mentioned to the post had a similar error and the solution, this threw me way off track and I was convinced that the error was with the Server and not with my application.

    Thanks a lot for your help.

    Saurabh Gupta

    P.S. You did mention permissions as one of the cause but I simply added NETWORK SERVICE to global admins to check and it didnt work. But the permissions for Exchange Server are different from the AD. I *should* have thought of that.

    Monday, May 29, 2006 6:35 AM
  • User843746731 posted

    Hi Saurabh,

    Excellent! Glad you got it sorted, I know the feeling with the one little permissions error (I had a scheduled script in vbs that used the command prompt, worked fine in 2000, but 2003 and it just didn't happen at all... cmd.exe is permissioned seperately in 2k3!).

    Hopefully this will help a few others as well now :D

    Tuesday, May 30, 2006 4:16 AM
  • User-340489618 posted
    Has anyone been able to create mailbox in 2003 (6.5) exchange? 
    Friday, September 15, 2006 9:53 AM