创建Exchange邮箱用户

概要
本分步指南介绍了如何使用 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:代码如下:
ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using CDOEXM;
using System.DirectoryServices;

namespace MBTest
{
    
class Class1
    {
        [STAThread]
        
static void Main(string[] args)
        {
            
string defaultNC = "DC=GROUPPOWER,DC=local";//根据域来改变值
            string alias = "dingfeng";
            
string fullName = "wudingfeng";
            
string password = "TestMb123.";//注意密码复杂度
            string domainName = "grouppower.local";
            
/*string homeMDB = "CN=邮箱存储(服务器名),CN=第一个存储组,"
                + "CN=InformationStore,CN=服务器名,CN=Servers,"
                + "CN=第一个管理组,CN=Administrative Groups,"
                + "CN=第一个组织,CN=Microsoft Exchange,CN=Services,"
                + "CN=Configuration,DC=域名,DC=域名后缀";
*/
            
string homeMDB = "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 = 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 });
            user.Properties[
"userAccountControl"].Value = 0x200//ADS_UF_NORMAL_ACCOUNT
            user.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 属性启用用户:
ContractedBlock.gif ExpandedBlockStart.gif Code
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 组织中的一个邮箱存储,调用 DirectoryEntry 上的 CommitChanges 以保存此新邮箱:
 
ContractedBlock.gif ExpandedBlockStart.gif 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.

 

转载于:https://www.cnblogs.com/wudingfeng/archive/2008/09/24/1298120.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值