System TipThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
This article was previously published under Q313114
This step-by-step article describes how to create a
mailbox-enabled user with the System.DirectoryServices namespace and CDO for Exchange Management (CDOEXM).
In Visual C# 2005 or in Visual C# .NET, create a new C#
console program that is named MBTest.
In Solution Explorer, right-click
References, and then click Add
Reference.
On the .NET tab, add a project reference
to the System.DirectoryServices namespace.
On the COM tab, add a reference to
Microsoft CDO for Exchange Management.
Replace the code in the Class1.cs file with the following
code.
Note In Visual C# 2005, replace the code in the Program.cs file
instead.
using System;
using CDOEXM;
using System.DirectoryServices;
namespace MBTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
//TODO: Change these items to values for your domain or organization.
string defaultNC = "DC=yourdomain,DC=com";
string alias = "jsmith";
string fullName = "Joseph Smith";
string password = "TestMb123.";
string domainName = "yourdomain.com";
string homeMDB = "CN=Mailbox Store (Your Server),CN=Your Storage Group,"
+ "CN=InformationStore,CN=Your Server,CN=Servers,"
+ "CN=Your Administrative Group,CN=Administrative Groups,"
+ "CN=Your Org,CN=Microsoft Exchange,CN=Services,"
+ "CN=Configuration,DC=Yourdomain,DC=Com";
DirectoryEntry container, user;
CDOEXM.IMailboxStore mailbox;
//This creates the new user in the "users" container.
//Set the sAMAccountName and the password
container = new DirectoryEntry("LDAP://cn=users," + defaultNC);
user = container.Children.Add("cn=" + fullName, "user");
user.Properties["sAMAccountName"].Add(alias);
user.CommitChanges();
user.Invoke("SetPassword", new object[]{password});
//This enables the new user.
user.Properties["userAccountControl"].Value = 0x200; //ADS_UF_NORMAL_ACCOUNT
user.CommitChanges();
//Obtain the IMailboxStore interface, create the mailbox, and commit the changes.
mailbox = (IMailboxStore)user.NativeObject;
mailbox.CreateMailbox(homeMDB);
user.CommitChanges();
return;
}
}
}
Change the variables in the TODO section in the Main function so that they contain the correct values for your
domain.
Compile the project, and then run the program.
Confirm that the new account was created in the domain by
starting the Active Directory Users and Computers snap-in in Microsoft
Management Console (MMC). You see the new user in the Users container. To
verify that this user is mailbox-enabled, view the user's properties and note
that the Exchange tabs appear, and that a mailbox store is
listed for the user on the Exchange General tab.
This code demonstrates how to bind to a container (in this case,
the Users container), and how to create a new user in the container. Do not
forget the "cn=" entry for the new user's name.
container = new DirectoryEntry("LDAP://cn=users," + defaultNC);
user = container.Children.Add("cn=" + fullName, "user");
Set properties on the new user
Assign a value for sAMAccountName. This is a mandatory attribute. The user account is not created
if you do not specify a value.
Because you have supplied the mandatory attributes, call CommitChanges to save the new user in the directory.
Call IADs::SetPassword to set the password. You must do this after a call to CommitChanges.
Enable the user by modifying the userAccountControl attribute.
user.Properties["sAMAccountName"].Add(alias);
user.CommitChanges();
user.Invoke("SetPassword", new object[]{password});
//This enables the new user:
user.Properties["userAccountControl"].Value = 0x200; //ADS_UF_NORMAL_ACCOUNT
user.CommitChanges();
Create a new mailbox
To get the IMailboxStore interface, cast DirectoryEntry.NativeObject to this type. This cast does not succeed at run time if CDOEXM is
not installed on a computer.
Call the CreateMailbox method, and pass a valid distinguished name to a mailbox store in
your Exchange organization.
Call CommitChanges on DirectoryEntry to save the new mailbox.
//Obtain the IMailboxStore interface, create the mailbox, and commit the changes.
mailbox = (IMailboxStore)user.NativeObject;
mailbox.CreateMailbox(homeMDB);
user.CommitChanges();
You must have appropriate permissions in the domain to
create a user and a mailbox. Typically, to create mailbox-enabled users in a
Windows 2000-based domain, you must be a member of the Windows 2000 Domain
Administrators group for the domain.
If this code runs on a computer other than an Exchange 2000
Server-based computer, you must have Exchange 2000 System Management Tools
installed on the computer. If you do not, CDOEXM is not available and the cast
to the IMailboxStore interface throws an InvalidCastException response:
An unhandled exception of type 'System.InvalidCastException'
occurred in MBTest.exe Additional information: Specified cast is not
valid.
If you receive an error message on the call to IMailboxStore.CreateMailbox, verify that the parameter that you passed to this method is a
valid mailbox store in your organization. If it is not, you receive an error
message that is similar to the following:
An unhandled
exception of type 'System.Runtime.InteropServices.COMException' occurred in
MBTest.exe Additional information: There is no such object on the server.