달력

32024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
출처 : http://support.microsoft.com/?kbid=313114


Create a new C# program

  1. In Visual C# 2005 or in Visual C# .NET, create a new C# console program that is named MBTest.
  2. In Solution Explorer, right-click References, and then click Add Reference.
  3. On the .NET tab, add a project reference to the System.DirectoryServices namespace.
  4. On the COM tab, add a reference to Microsoft CDO for Exchange Management.
  5. Replace the code in the Class1.cs file with the following code.

    Note In Visual C# 2005, replace the code in the Program.cs file instead.
    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. Change the variables in the TODO section in the Main function so that they contain the correct values for your domain.
  7. Compile the project, and then run the program.
  8. Confirm that the new account was created in the domain by starting the Active Directory Users and Computers snap-in in Microsoft Management Console (MMC). You see the new user in the Users container. To verify that this user is mailbox-enabled, view the user's properties and note that the Exchange tabs appear, and that a mailbox store is listed for the user on the Exchange General tab.

Code description

Create a new DirectoryEntry

This code demonstrates how to bind to a container (in this case, the Users container), and how to create a new user in the container. Do not forget the "cn=" entry for the new user's name.
container = new DirectoryEntry("LDAP://cn=users," + defaultNC);
user = container.Children.Add("cn=" + fullName, "user");
				

Set properties on the new user

  1. Assign a value for sAMAccountName. This is a mandatory attribute. The user account is not created if you do not specify a value.
  2. Because you have supplied the mandatory attributes, call CommitChanges to save the new user in the directory.
  3. Call IADs::SetPassword to set the password. You must do this after a call to CommitChanges.
  4. Enable the user by modifying the userAccountControl attribute.
    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();
    				

Create a new mailbox

  1. To get the IMailboxStore interface, cast DirectoryEntry.NativeObject to this type. This cast does not succeed at run time if CDOEXM is not installed on a computer.
  2. Call the CreateMailbox method, and pass a valid distinguished name to a mailbox store in your Exchange organization.
  3. Call CommitChanges on DirectoryEntry to save the new mailbox.
    //Obtain the IMailboxStore interface, create the mailbox, and commit the changes.
    mailbox = (IMailboxStore)user.NativeObject;
    mailbox.CreateMailbox(homeMDB);
    user.CommitChanges();
    				

Troubleshooting

  • You must have appropriate permissions in the domain to create a user and a mailbox. Typically, to create mailbox-enabled users in a Windows 2000-based domain, you must be a member of the Windows 2000 Domain Administrators group for the domain.
  • If this code runs on a computer other than an Exchange 2000 Server-based computer, you must have Exchange 2000 System Management Tools installed on the computer. If you do not, CDOEXM is not available and the cast to the IMailboxStore interface throws an InvalidCastException response:
    An unhandled exception of type 'System.InvalidCastException' occurred in MBTest.exe
    Additional information: Specified cast is not valid.
  • If you receive an error message on the call to IMailboxStore.CreateMailbox, verify that the parameter that you passed to this method is a valid mailbox store in your organization. If it is not, you receive an error message that is similar to the following:
    An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in MBTest.exe
    Additional information: There is no such object on the server.
Posted by tornado
|