首页 > 开发 > 综合 > 正文

Creating E2K Mailbox with Directory Services

2024-07-21 02:24:36
字体:
来源:转载
供稿:网友
how to: create a mailbox-enabled recipient by using c# .net
the information in this article applies to:
microsoft visual c# .net (2002)
microsoft collaboration data objects for exchange management (cdoexm)
microsoft exchange 2000 server
microsoft exchange 2000 enterprise server
microsoft active directory client extension
microsoft active directory services interface, system component

this article was previously published under q313114
in this task
summary

requirements
create a new c# program
code description

create a new directoryentry
set properties on the new user
create a new mailbox
troubleshooting
references
summary
this step-by-step article describes how to create a mailbox-enabled user with the system.directoryservices namespace and cdo for exchange management (cdoexm).

back to the top
requirements
the following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
a microsoft windows 2000-based domain with exchange 2000 installed
visual c# .net
microsoft exchange 2000 system management tools on the computer on which this code runs
back to the top
create a new c# program
in visual c# .net, create a new c# console program that is named mbtest.
in solution explorer, right-click references, and then click add reference.
on the .net tab, add a project reference to system.directoryservices.
on the com tab, add a reference to microsoft cdo for exchange management.
replace the code in class1.cs with the following code: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;
          }
     }
}
                    
change the variables in the todo section in the main function so that they contain proper values for your domain.
compile the project and then run the program.
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.
back to the top
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");
                
back to the top
set properties on the new user
assign a value for samaccountname. this is a mandatory attribute; the user account is not created if you do not specify a value. because you have supplied the mandatory attributes, call commitchanges to save the new user in the directory. next, call iads::setpassword to set the password. you must do this after a call to commitchanges. finally, 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();
                
back to the top
create a new mailbox
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. call the createmailbox method, and pass a valid distinguished name to a mailbox store in your exchange organization. finally, 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();
                
back to the top
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 imailboxstore 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:

an unhandled exception of type 'system.runtime.interopservices.comexception' occurred in mbtest.exe
additional information: there is no such object on the server.
back to the top
references
for more information about system.directoryservices, visit the following microsoft web site:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdirectoryservices.asp

for more information about cdoexm, visit the following microsoft web site:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wss/wss/_esdk_reference_cdoexm.asp

back to the top
last reviewed: 10/26/2002
keywords: kbhowto kbhowtomaster kb313114 kbauddeveloper

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表