Microsoft KB Archive/313114

= How to create a mailbox-enabled recipient by using Visual C# =

Article ID: 313114

Article Last Modified on 5/18/2007

-

APPLIES TO


 * Microsoft Visual C# 2005
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Collaboration Data Objects for Exchange Management 1.1
 * Microsoft Exchange 2000 Server Standard Edition
 * Microsoft Exchange 2000 Enterprise Server
 * Microsoft Active Directory Client Extension
 * Microsoft Active Directory Service Interfaces 2.5

-



This article was previously published under Q313114





SUMMARY
This step-by-step article describes how to create a mailbox-enabled user with the System.DirectoryServices namespace and CDO for Exchange Management (CDOEXM).

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * A Microsoft Windows 2000-based domain that has Microsoft Exchange 2000 installed
 * Microsoft Visual C# 2005 or Microsoft Visual C# .NET
 * Microsoft Exchange 2000 System Management Tools on the computer on which this code runs



Create a new C# program
 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 = &quot;DC=yourdomain,DC=com&quot;; string alias = &quot;jsmith&quot;; string fullName = &quot;Joseph Smith&quot;; string password = &quot;TestMb123.&quot;; string domainName = &quot;yourdomain.com&quot;; string homeMDB = &quot;CN=Mailbox Store (Your Server),CN=Your Storage Group,&quot; + &quot;CN=InformationStore,CN=Your Server,CN=Servers,&quot; + &quot;CN=Your Administrative Group,CN=Administrative Groups,&quot; + &quot;CN=Your Org,CN=Microsoft Exchange,CN=Services,&quot; + &quot;CN=Configuration,DC=Yourdomain,DC=Com&quot;;

DirectoryEntry container, user; CDOEXM.IMailboxStore mailbox;

//This creates the new user in the &quot;users&quot; container. //Set the sAMAccountName and the password container = new DirectoryEntry(&quot;LDAP://cn=users,&quot; + defaultNC); user = container.Children.Add(&quot;cn=&quot; + fullName, &quot;user&quot;); user.Properties[&quot;sAMAccountName&quot;].Add(alias); user.CommitChanges; user.Invoke(&quot;SetPassword&quot;, new object[]{password}); //This enables the new user. user.Properties[&quot;userAccountControl&quot;].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.</li> Compile the project, and then run the program.</li> 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.</li></ol>

Create a new DirectoryEntry
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 &quot;cn=&quot; entry for the new user's name. container = new DirectoryEntry(&quot;LDAP://cn=users,&quot; + defaultNC); user = container.Children.Add(&quot;cn=&quot; + fullName, &quot;user&quot;);

Set properties on the new user
<ol> Assign a value for sAMAccountName. This is a mandatory attribute. The user account is not created if you do not specify a value.</li> Because you have supplied the mandatory attributes, call CommitChanges to save the new user in the directory.</li> Call IADs::SetPassword to set the password. You must do this after a call to CommitChanges.</li>  Enable the user by modifying the userAccountControl attribute. user.Properties[&quot;sAMAccountName&quot;].Add(alias); user.CommitChanges; user.Invoke(&quot;SetPassword&quot;, new object[]{password}); //This enables the new user: user.Properties[&quot;userAccountControl&quot;].Value = 0x200; //ADS_UF_NORMAL_ACCOUNT user.CommitChanges; </li></ol>

Create a new mailbox
<ol> 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.</li> Call the CreateMailbox method, and pass a valid distinguished name to a mailbox store in your Exchange organization.</li>  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; </li></ol>

Troubleshooting
<ul> 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.</li> 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.

</li> 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.

</li></ul>

<div class="references_section">