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;
}
}
}
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();
疑难解答
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.