Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->网络应用 ->邮件服务器 ->Exchange Server ->正文

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

来源:Linuxdby.com 作者:Webmaster 时间:2007-05-28 点击: [收藏] [投稿]

概要

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

返回页首

要求

下表列出了推荐使用的硬件、软件、网络基础结构以及所需的 Service Pack:
%26#8226;安装了 Exchange 2000 的一个基于 Microsoft Windows 2000 的域
%26#8226;Visual C# .NET
%26#8226;在此代码运行的计算机上有 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_ACCOUNTuser.CommitChanges();				

创建新邮箱



 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:空间不足引起Exchange Server罢工   下一篇:如何将OWA的HTTP连接重定向到 HTTPS

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章