硬件学院 | 网络学院 | 游戏秘籍 | 求职技巧 | 企业管理 | 软件资讯 | IT导购 | 软件下载 | 源码下载
软件学院 | 安全资讯 | 图形图象 | 网络营销 | 电子商务 | 硬件资讯 | IT生活 | 教程下载 | 电影娱乐
网站首页    个人求职    单位招聘    高校联盟    猎头服务    培训服务    资讯中心    IT论坛
让每一个热爱IT的人都找到一份满意的工作!
文章搜索:
 您的位置首页->-> 软件学院-> .NET技术-> Creating E2K Mailbox with Directory Services
Creating E2K Mailbox with Directory Services
作者:中国资讯网 来源:zixuen.com 加入时间:2005-5-12 www.cnitrc.com
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


  相关文章:
.NET技术
ASP技术
PHP技术
JSP技术
.NET技术
服务器技术
数据库技术
其它类
工具软件
办公软件
本类阅读TOP10
 
关于我们   |   服务声明   |   使用帮助   |   广告合作   |   网站地图   |   友情链接   |   加盟合作   |   联系我们
Copyright © 2006 cnitrc.com Inc. All Rights Reserved. 浙ICP备05074295号
中国IT人才网 版权所有 网络实名:中国IT人才
未经书面授权严禁转载和复制本站的任何招聘信息和文章