Creating E2K Mailbox with Directory Services

來源:互聯網
上載者:User
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 2000 Server
Microsoft Exchange 2000 Enterprise Server
Microsoft Active Directory Client Extension
Microsoft Active Directory Services Interface, System Component

This article was previously published under Q313114
IN 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 how to create a mailbox-enabled user with the System.DirectoryServices namespace and CDO for Exchange Management (CDOEXM).

back to the top
Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
A Microsoft Windows 2000-based domain with Exchange 2000 installed
Visual C# .NET
Microsoft Exchange 2000 System Management Tools on the computer on which this code runs
back to the top
Create a New C# Program
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 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 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 proper 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.
back to the top
Code Description
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 "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 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. Next, call IADs::SetPassword to set the password. You 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 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. Finally, 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();
                
back to the top
Troubleshooting
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 IMailboxStore 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:

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 the 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 the 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



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。