in

ASP.NET Weblogs

Brian Desmond's Blog

Inherits Network.Admin
Implements IOneManBand

Mailbox Enabling a User in .Net

A while back, I promised I'd continue talking about CDO, .Net, Exchange, AD, and good stuff like that. Albeit belatedly, I thought I'd post how to mailbox enable a user in VB.Net.

I'm goign to assuem that the user is already in the directory, as we already covered creating objects a while back. Inorder to do this, you're goign to need the Exchange Management Tools installed on your dev box, as a COM object does the work here.

In your VS project, you'll need to add a reference to the “Microsoft CDO for Exchange Management Library” (aka CDOEXM). You're also going to need the full distinguished name of the exchange mailbox store you're planning to create the mailbox in. This can be a hassle to get right if you've never tried to type out the path by hand. The easiest way to find the path, copy & paste ready is to use the handy ldp utility included with any server OS (see my previous blog entry on using this utility). The data is in the Configuration/Services/Microsoft Exchange/Your Org/Administrative Groups/Your Admin Group/Servers/Your Server/Information Store/Information Store Name/MailboxStoreName tree.

There's actually only a couple of lines of code involved in doing the actual mailbox creation. The code is below, and then I'll explain briefly below:

>>>>>
Option Strict Off
Imports System.DirectoryServices
Imports CDOEXM

Dim user As New DirectoryEntry(”LDAP://cn=john doe,cn=users,dc=mydomain,dc=local”)
Dim mbx as IMailboxStore = user.NativeObject

mbx.CreateMailbox(”CN=My Mailbox Store (MyServer),CN=Information Store,CN=InformationStore,CN=MyServer,CN=Servers,CN=MyAdminGroup,CN=Administrative Groups,CN=MyOrganization,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=mydomain,DC=local”)
user.CommitChanges
<<<<<<<

So, what we've done here is quite simple: First, we bound to the user in the directory, and then we created a IMailboxStore object representing the user. We then called IMailboxStore.CreateMailbox, passing the distinguished name of the mailbox store to it. We saved the changes to the user object, and, voila, mailbox created! The IMailboxStore object has a bunch of other properties, items such as quotas and other odd's and ends. They're pretty self explanatory if explored through intellisense, and there are full docs in MSDN.

Published Apr 10 2004, 03:29 PM by bdesmond
Filed under: ,

Comments

 

Emerson said:

Where I find the package CDOEXM?
Because I don't get to find in "Add Reference" of Visual .Net



April 26, 2004 8:47 AM
 

Henry said:

I tried this but I get the error:
An unhandled exeption from the type 'System.Runtime.InteropServices.COMException' occured in system.directoryservices.dll.
Additional information: Unknown Error(0x80040e37)

The error occured in the line "Dim mbx As IMailboxStore = user.NativeObject"

How can I handle this.

PS: Sorry for the english. I translated the messages from german
April 29, 2004 12:23 PM
 

aldousd666 said:

hey, thanks a lot for the code. I wrote this code into a web service that does nothing but create and delete mailboxes. Now if I can only figure out how to call this service with delegated credentials from another asp web-application, I'll be in business. Running the service from IE logged in as myself works great. I'll check back with you if I get that working, because others could probably find that sort of thing useful.

Of note already: The worker process must run with the credentials of a user with exchange create rights -- you cannot simply invoke the New DirectoryEntry("path", "user", "pass") and expect the CDOEXM to do the rest, because CDOEXM (by design) will only pass the creds of the process itself not the ADSI provider. I got around it by enabling impersonation, disabling anon access in IIS snap-in.
July 12, 2004 3:20 PM
 

TrackBack said:

^_^,Pretty Good!
April 10, 2005 3:46 AM
 

Sony said:

I was able to mail enable an already existing user in AD

Try this....

Public Shared Function CreateMailbox(ByRef strBuilderStatus As StringBuilder, ByVal deUser As DirectoryEntry, ByVal strExchangeServerString As String, ByVal strRole As String) As String

       'This function will mailbox enable a user whom already exists

       Dim homeMDB As String = "CN=" + strRole + "," + strExchangeServerString

       'See if the user exists first

       'It Worked: Dim exUser As DirectoryEntry = New DirectoryEntry("LDAP://AXABS-IN.intraxa/CN=Siena Honda,OU=Test,OU=AXADaemon,OU=Systems and Admins,OU=User Accounts,OU=Business Community,DC=axabs-in,DC=intraxa")

       strBuilderStatus.Append("deUser.Path:" + deUser.Path)

       Dim exUser As DirectoryEntry = New DirectoryEntry(deUser.Path)

       'Dim exUser As DirectoryEntry = FindUser(UserName)

       If Not (Nothing Is exUser) Then

           strBuilderStatus.Append("exUser is not nothing,exUser.Path " + exUser.Path)

           Try

               Dim prsmbx As CDO.Person

               Dim mbx As IMailboxStore

               prsmbx = New CDO.Person

               strBuilderStatus.Append("Got Person object for all,Opening Path,exUser.Path:" + exUser.Path)

               'It worked:prsmbx.DataSource.Open("LDAP://AXABS-IN.intraxa/CN=Loka Priyan,OU=Test,OU=AXADaemon,OU=Systems and Admins,OU=User Accounts,OU=Business Community,DC=axabs-in,DC=intraxa", , ADODB.ConnectModeEnum.adModeReadWrite)

               prsmbx.DataSource.Open(exUser.Path, , ADODB.ConnectModeEnum.adModeReadWrite)

               mbx = prsmbx

               ''mbx.CreateMailbox( _

               ''"LDAP://domain.com/CN=Store,CN=StorageGroup," & _

               ''"CN=InformationStore,CN=ServerName,CN=Servers," & _

               ''"CN=First Administrative Group,CN=Administrative Groups," & _

               ''"CN=Domain,CN=Microsoft Exchange,CN=Services," & _

               ''"CN=Configuration,DC=domain,DC=com")

               strBuilderStatus.Append("Creating..")

               'mbx.CreateMailbox("CN=Managers,CN=Third Storage Group,CN=InformationStore,CN=PRUIN01EX02,CN=Servers,CN=axaabscoin,CN=Administrative Groups,CN=Axa Business Services,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=adsin,DC=intraxa")

               strBuilderStatus.Append("homeMDB Ideal Format(which works):#CN=Managers,CN=Third Storage Group,CN=InformationStore,CN=PRUIN01EX02,CN=Servers,CN=axaabscoin,CN=Administrative Groups,CN=Axa Business Services,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=adsin,DC=intraxa#")

               strBuilderStatus.Append("homeMDB Actual:#" + homeMDB + "#")

               mbx.CreateMailbox(homeMDB)

               ' Set some additional mail fields

               strBuilderStatus.Append("Created..")

               With prsmbx

                   'If Domain.ToLower = "aodinc.com" Then

                   '    .Email = "SMTP:" & UserName & "@domain.com"

                   'Else

                   '    .Email = "SMTP:" & UserName & "@" & Domain

                   '    .Email2 = "smtp:" & UserName & "@domain.com"

                   'End If

                   strBuilderStatus.Append("Calling prsmbx.Fields.Update ")

                   .Fields.Update()

                   strBuilderStatus.Append("Called  prsmbx.Fields.Update ")

                   strBuilderStatus.Append("Calling prsmbx.DataSource.Save ")

                   .DataSource.Save()

                   strBuilderStatus.Append("Called  prsmbx.DataSource.Save ")

               End With

           Catch ex As Exception

               strBuilderStatus.Append("Exception Occured:" + ex.Message + "#" + ex.StackTrace)

               Return "FAIL"

           End Try

       Else

           strBuilderStatus.Append("exUser is nothing ")

           Return "FAIL"

       End If

       Return "SUCCESS"

   End Function

September 11, 2007 5:23 AM
 

Glen Eastlake said:

Great info Desmond. Can anybody explain to me how I can connect to a MS exchange server and import the mailbox email addresses into a VB.net application ?

Or could you point me in the right direction of where I can find this information out?

Thanks in advance for any help

October 11, 2007 9:58 PM

Leave a Comment

(required)  
(optional)
(required)  
Add