使用C# .NET创建启用了Exchange邮箱收件人

概要

本分步指南介绍了如何使用 System.DirectoryServices 命名空间和 CDO for Exchange Management (CDOEXM) 创建一个启用了邮箱的用户。

返回页首

要求

下表列出了推荐使用的硬件、软件、网络基础结构以及所需的 Service Pack:
安装了 Exchange 2000 的一个基于 Microsoft Windows 2000 的域
Visual C# .NET
在此代码运行的计算机上有 Microsoft Exchange 2000 系统管理工具
 

创建新的 C# 程序

1. 在 Visual C# .NET 中,新建一个名为 MBTest 的 C# 控制台程序。
2. 在解决方案资源管理器中,右键单击“引用”,然后单击“添加引用”。
3. 在“.NET”选项卡上,添加一个到 System.DirectoryServices 的项目引用。
4. 在“COM”选项卡上,添加一个到“Microsoft CDO for Exchange Management”的引用。
5. 将 Class1.cs 中的代码替换为下面的代码:
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;
          }
     }
}
					
6. 更改 Main 函数的 TODO 部分中的变量,使它们包含针对您的域的适当的值。
7. 编译此项目,然后运行该程序。
8. 启动 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 属性启用用户:
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();
				

创建新邮箱

1. 为获得 IMailboxStore 接口,请将 DirectoryEntry.NativeObject 强制转换为此类型。如果计算机上没有安装 CDOEXM,此强制转换在运行时将不能成功。
2. 调用 CreateMailbox 方法,并将一个有效的可分辨名称传递给您的 Exchange 组织中的一个邮箱存储。
3. 调用 DirectoryEntry 上的 CommitChanges 以保存此新邮箱:
//Obtain the IMailboxStore interface, create the mailbox, and commit the changes.
mailbox = (IMailboxStore)user.NativeObject;
mailbox.CreateMailbox(homeMDB);
user.CommitChanges();
				

疑难解答

您必须在域中有适当的权限才能创建用户和邮箱。通常情况下,要在一个基于 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.
如果您调用 IMailboxStore.CreateMailbox 时收到一条错误信息,请确认您传递给此方法的参数是不是您的组织中一个有效的邮箱存储。如果不是,您将收到类似于下面的错误信息:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in MBTest.exe
Additional information:There is no such object on the server.
, ,

分享到