概要
本分步指南介绍了如何使用 System.DirectoryServices 命名空间和 CDO for Exchange Management (CDOEXM) 创建一个启用了邮箱的用户
需要
1:安装了 Exchange 2000 的一个基于 Microsoft Windows 2000 的域,配置如下:
2:在此代码运行的电脑上有 Microsoft Exchange 2000 系统管理工具
创建新的 C# 程式
1: 在 Visual C# .NET 中,新建一个名为 MBTest 的 C# 控制台程式
2:添加一个System.DirectoryServices 的.net引用
3:添加一个到Microsoft CDO for Exchange Management的COM引用
4:代码如下:
CodeusingSystem;usingCDOEXM;usingSystem.DirectoryServices;namespaceMBTest
{classClass1
{
[STAThread]staticvoidMain(string[] args)
{stringdefaultNC="DC=GROUPPOWER,DC=local";//根据域来改变值stringalias="dingfeng";stringfullName="wudingfeng";stringpassword="TestMb123.";//注意密码复杂度stringdomainName="grouppower.local";stringhomeMDB="CN=邮箱存储(client),CN=第一个存储组,CN=InformationStore,CN=client,CN=Servers,CN=第一个管理组,CN=Administrative Groups,CN=Grouppower,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=grouppower,DC=local";
DirectoryEntry container=null, user=null;
CDOEXM.IMailboxStore mailbox;//创建具有用户组权限的用户和密码container=newDirectoryEntry("LDAP://cn=users,"+defaultNC);
user=container.Children.Add("cn="+fullName,"user");
user.Properties["sAMAccountName"].Add(alias);
user.CommitChanges();
user.Invoke("SetPassword",newobject[] { password });
user.Properties["userAccountControl"].Value=0x200;//ADS_UF_NORMAL_ACCOUNTuser.CommitChanges();
mailbox=(IMailboxStore)user.NativeObject;//Obtain the IMailboxStore interface, create the mailbox, and commit the changes.mailbox.CreateMailbox(homeMDB);
user.CommitChanges();return;
}
}
}
5:更改 Main 函数的 TODO 部分中的变量,使他们包含针对您的域的适当的值
6:编译此项目,然后运行
7:启动 Microsoft 管理控制台 (MMC) 中的“Active Directory 用户和电脑”管理单元,确认是否已在域中创建了新帐户。您会在“用户”容器中看到此新用户。如要检查此用户是否启用了邮箱,请查看该用户的属性中是否出现了“Exchange”选项卡,连同“Exchange 常规”选项卡上是否为该用户列出了一个邮箱存储
代码说明
创建新的 DirectoryEntry
此代码演示了如何绑定到容器(在本例中为“用户”容器),连同如何在该容器中创建一个新用户。不要忘记表示新用户名的“cn=”项:
container = new DirectoryEntry("LDAP://cn=users," + defaultNC);user = container.Children.Add("cn=" + fullName, "user");
在新用户上配置属性
1:给 sAMAccountName 赋一个值。这是个必需属性;假如您不指定值,就不会创建用户帐户
2:因为您已提供了必需属性,所以要调用 CommitChanges 将新用户保存到目录中
3:调用 IADs::SetPassword 以配置密码。调用 CommitChanges 之后必须这样做
4:通过修改 userAccountControl 属性启用用户:
Codeuser.Properties["sAMAccountName"].Add(alias);
user.CommitChanges();
user.Invoke("SetPassword",newobject[] { password });//This enables the new user.user.Properties["userAccountControl"].Value=0x200;//ADS_UF_NORMAL_ACCOUNTuser.CommitChanges();
创建新邮箱
1:为获得 IMailboxStore 接口,请将 DirectoryEntry.NativeObject 强制转换为此类型。假如电脑上没有安装 CDOEXM,此强制转换在运行时将不能成功.
2:调用 CreateMailbox 方法,并将一个有效的可分辨名称传递给您的 Exchange 组织中的一个邮箱存储,调用 DirectoryEntry 上的 CommitChanges 以保存此新邮箱:
Code//Obtain the IMailboxStore interface, create the mailbox, and commit the changes.mailbox=(IMailboxStore)user.NativeObject;
mailbox.CreateMailbox(homeMDB);
user.CommitChanges();
疑难解答
1:您必须在域中有适当的权限才能创建用户和邮箱。通常情况下,要在一个基于 Windows 2000 的域中创建一个启用了邮箱的用户,您必须是该域的 Windows 2000 域管理员组中的一名成员。假如此代码在不是基于 Exchange 2000 Server 的电脑上运行,您必须在该电脑上安装 Exchange 2000 系统管理工具。假如不这样做,则 CDOEXM 将不可用,而且向 IMailboxStore 的强制转换将导致返回 InvalidCastException 响应:
An unhandled exception of type 'System.InvalidCastException' occurred in MBTest.exe
Additional information:Specified cast is not valid
2:假如您调用 IMailboxStore.CreateMailbox 时收到一条错误信息,请确认您传递给此方法的参数是不是您的组织中一个有效的邮箱存储。假如不是,您将收到类似于下面的错误信息:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in MBTest.exe bitsCN.Com
Additional information:There is no such object on the server.



