文章編號: 313114 - 上次校閱: 2003年5月26日 - 版次: 1.0

HOW TO:使用 C#.NET 建立已啟用信箱的收件者

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

本文逐步說明如何用 System.DirectoryServices 命名空間與 Exchange Management (CDOEXM) CDO 建立已啟用信箱的使用者。

需求

下面清單提列了建議使用的硬體、軟體、網路基礎架構以及所需安裝的 Service Pack:
  • 安裝了 Exchange 2000 的 Microsoft Windows 2000 網域
  • Visual C# .NET
  • 執行這支程式碼的電腦上要有 Microsoft Exchange 2000 系統管理工具

Create a New C# Program

  1. 在 Visual C# .NET,建立一個新的 C# 控制台程式,它命名為 MBTest。
  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 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 網站:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemTextStringBuilderClassTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDirectoryServices.asp)
如需 ASP.NET 的詳細資料,請造訪下列 Microsoft 網站:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndive/html/data08092001.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wss/wss/_esdk_reference_cdoexm.asp)

這篇文章中的資訊適用於:
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Collaboration Data Objects for Exchange Management 1.1
  • Microsoft Exchange 2000 Server Standard Edition
  • Microsoft Exchange 2000 Enterprise Server
  • Microsoft Active Directory Client Extension
  • Microsoft Active Directory Service Interfaces 2.5
關鍵字:?
kbhowto kbhowtomaster KB313114
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。