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.
Sometimes remembering or deriving the full distinguished name of an object in the directory can be painful to do by hand. Luckily, Microsoft provides a nice little browser utility with the server os' and the 2003 admin pack. Beware that you can do A LOT of damage with the utility, so, my recommendation is to use standard user credentials when binding - you could delete a nice chunk of the directory tree without much effort!
So, open up the utility (start>run>ldp), and then goto File>connect, and up in the FQDN of the domain you want to bind to (e.g. mycompany.local). Next, goto File>bind, and put in some domain credentials - stadnard user ones are more than enough and highly recommended! The third step is going to View>tree, and then just press OK. If you're in a large domain, you might want to specify the DN of a root to cut down on DC load.
Now you can browse to the object you need the DN of. Find it, and double click. On the right, if you scroll up a bit, you'll find all the object's attributes, including it's DN. Highlight it, press Ctrl + C, and paste it in whereever useful