Monday, June 22, 2009

How to create Mailbox in Exchange Server 2007 programmatically

If you have exchange server 2007 or you want to upgrade from exchange server 2003 to exchange server 2007. And you want your end user to create his email on exchange from your application or portal. Of course in exchange 2003 we can use CDOEXM to create mailboxes programmatically. But in exchange server 2007 doesn't support CDOEXM. Therefore to do this, we should depend on Power Shell in creating mailboxes.

The prerequisites are just to install power shell on your web server and of course you need to install the Exchange Server Management Tools on the server that will execute the code.
After that you can use this function:

public static IList CreateEmail(string userName, string mail, string domain, string ou, string exDatabase)
{
//create the command
string cmd = string.Format("Enable-Mailbox -Identity '{0}/{1}/{2}' -Alias '{3}' -Database '{4}'", domain, ou, userName, mail, exDatabase);
ExchangeManagementShellWrapper ems = ExchangeManagementShellWrapper.Instance;
ICollection results;
IList IErrors;

results = ems.RunspaceInvoke(cmd, out IErrors);

return IErrors;

}

Of course this function needs user name, mail (the name that you want to be user email), domain, ou (which represents the organization unit path, which this user is existed on), and exchange database. And by using ExchangeManagementShellWrapper you can run this command and get the result.

for source code of ExchangeManagmentShellWrapper, you can download it from internet or anyone who need it,I can send it to him by email.

No comments: