Services How to:create a mailbox-enabled recipient by Using C #. NET
The information in this article applies to:
Microsoft Visual C #. NET (2002)
Microsoft collaboration Data Objects for Exchange Management (CDOEXM)
Microsoft Exchange Server
Microsoft Exchange Enterprise Server
Microsoft Active Directory Client Extension
Microsoft Active Directory Services Interface, System Component
This is article was previously published under Q313114
In the This TASK
SUMMARY
Requirements
Create a New C # program
Code Description
Create a New DirectoryEntry
Set Properties on the New User
Create a New Mailbox
Troubleshooting
REFERENCES
SUMMARY
This step-by-step article describes you to create a mailbox-enabled user with the System.DirectoryServices namespace and C Do for Exchange Management (CDOEXM).
Back to the top
Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are D:
A Microsoft Windows 2000-based domain with Exchange installed
Visual C #. NET
Microsoft Exchange System Management Tools on the computer to which this code runs
Back to the top
Create a New C # program
In Visual C #. NET, create a new C # console program This is named Mbtest.
In Solution Explorer, right-click References, and then click Add Reference.
On the. NET tab, add a project reference to System.DirectoryServices.
On the COM tab, add a reference to Microsoft CDO for Exchange Management.
Replace the code in Class1.cs with the following code:using System;
Using CDOEXM;
Using System.DirectoryServices;
Namespace Mbtest
{
Class Class1
{
[STAThread]
static void Main (string[] args)
{
Todo:change 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";
This is 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
}
}
}
The change of the variables in the "TODO" section in the Main function, so, they contain proper values for your domain.
Compile the project and then run the program.
Confirm that "new account is created in" Domain by starting the Active Directory Users and Computers snap-in in Mic Rosoft Management Console (MMC). You are the new user in the Users container. To verify which is mailbox-enabled, view the user's properties and note the Exchange tabs appear, and that a Mailbox store is listed for the "user on" The Exchange General tab.
Back to the top
Code Description
Create a New DirectoryEntry
This code demonstrates you to bind to a container (in this case, the Users container), and you to create a new user in the Container. Don't forget the "cn=" entry for the new user ' s Name:container = new DirectoryEntry ("Ldap://cn=users," + DEFAULTNC);
user = container. Children.add ("cn=" + fullName, "user");
Back to the top
Set Properties on the New User
Assign a value for sAMAccountName. This is a mandatory attribute; The user account isn't created if you don't specify a value. Because you have supplied the mandatory attributes, call CommitChanges to save the new user in the directory. Next, call Iads::setpassword to set the password. You are must do this after a call to CommitChanges. Finally, 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 ();
Back to the top
Create a New Mailbox
To get the IMailboxStore interface, cast directoryentry.nativeobject to this type. This cast does isn't succeed at the run time if CDOEXM isn't installed on a computer. Call the CreateMailbox method, and pass a valid distinguished name to a mailbox store in your Exchange organization. Finally, call CommitChanges on DirectoryEntry to save the new mailbox://obtain The IMailboxStore interface, create the MA Ilbox, and commit the changes
Mailbox = (imailboxstore) user. NativeObject;
Mailbox. CreateMailbox (HomeMDB);
User.commitchanges ();
Back to the top
Troubleshooting
You are 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 being a member of the Windows domain Administrators Group for the domain.
If This is code runs on a computer the other than a Exchange Server-based computer for you must have Exchange System Mana Gement Tools installed on the computer. If you don't, CDOEXM is isn't available and the cast to IMailboxStore throws a InvalidCastException response:
An unhandled exception of type ' System.InvalidCastException ' occurred in MBTest.exe
Additional information:specified cast is not valid.
If you receive the ' on ' call ' to Imailboxstore.createmailbox ', verify that ' parameter ' you passed to T He is a valid mailbox store in your organization. If It isn't, you receive the error message "is" similar to:
An unhandled exception of type ' System.Runtime.InteropServices.COMException ' occurred in MBTest.exe
Additional Information:there is no such object on the server.
Back to the top
REFERENCES
For more information about System.DirectoryServices, visit following Microsoft Web site:
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDirectoryServices.asp
For more information about CDOEXM, visit following Microsoft Web site:
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wss/wss/_esdk_reference_cdoexm.asp
Back to the top
Last reviewed:10/26/2002
Keywords:kbhowto Kbhowtomaster KB313114 kbAudDeveloper
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.