本文逐步說明如何用 System.DirectoryServices 命名空間與 Exchange Management (CDOEXM) CDO 建立已啟用信箱的使用者。
需求
下面清單提列了建議使用的硬體、軟體、網路基礎架構以及所需安裝的 Service Pack:
- 安裝了 Exchange 2000 的 Microsoft Windows 2000 網域
- Visual C# .NET
- 執行這支程式碼的電腦上要有 Microsoft Exchange 2000 系統管理工具
Create a New C# Program
- 在 Visual C# .NET,建立一個新的 C# 控制台程式,它命名為 MBTest。
- 在 [方案總管] 以右鍵按一下 [參考] 下的連結,再按一下 [加入參考]。
- 在 [NET] 索引標籤,新增一個專案參考到 System.DirectoryServices。
- 在 [COM] 索引標籤上,新增指向 Microsoft CDO for Exchange Management。
- 使用下面程式碼來取代 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;
}
}
}
- 在 [Main] 函數中改變 TODO 區段的變數,讓它們包含適合網域的值。
- 編譯專案並執行程式。
- 確認啟動 Microsoft Management Console (MMC) 中的 Active Directory 使用者和電腦嵌入式管理單元在網域中建立新帳戶。您會在 Users 容器中看到新的使用者。要確認使用者啟用了信箱,請檢視使用者的內容並注意 [交換] 索引標籤是否出現,而使用者的信箱儲存區會列在 [交換一般] 索引標籤上。
程式碼說明
建立新的 DirectoryEntry
這段程式碼顯示了如何繫結容器 (在這個案例中是 Users 容器),以及如何在容器中建立新的使用者。請不要漏掉新使用者名稱的 cn= 項目。
container = new DirectoryEntry("LDAP://cn=users," + defaultNC);
user = container.Children.Add("cn=" + fullName, "user");
設定新使用者的內容
為
sAMAccountName指定值。這是強制的屬性;假如您沒有指定值,就不會建立使用者帳戶。這是因為您提供強制屬性,呼叫
CommitChanges 將新的使用者儲存在目錄中。接下來,呼叫
IADs::SetPassword 以設定密碼。您必須在呼叫
CommitChanges之後進行這個步驟。最後,藉著修改
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();
建立新信箱
如果要取得 IMailboxStore 介面,請將 DirectoryEntry.NativeObject 轉換到這個類型。假如電腦上沒有安裝 CDOEXM,轉換會在執行期間失敗。呼叫
CreateMailbox 方法,並傳遞有效的可辨別名稱到 Exchange 組織的信箱儲存區。最後,在 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 Domain Administrators 群組的成員。
- 假如這個程式碼在非 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.
如需關於 System.DirectoryServices 的更多資訊,請造訪下列 Microsoft 網站:
如需 ASP.NET 的詳細資料,請造訪下列 Microsoft 網站: