313114 How to create a recipient (from mkba) that enables mailboxes using C # .NET

xiaoxiao2021-03-06  76

This task content

summary

Create a new C # program code description

Creating new DirectoryEntry Settings Properties on new users Creating new mailbox Troubleshooting Reference

SUMMARY This step-by-step guide describes how to create a user enabled mailbox using System.DirectoryServices Namespace and CDO for Exchange Management (CDOEXM).

Back to top

The following table is required to list the recommended hardware, software, network infrastructure, and the required service pack:

A Microsoft Exchange 2000 System Administration Tools on a Microsoft Windows 2000-based Visual C # .NET installed on this code.

Back to top

Create a new C # program

In Visual C # .NET, create a C # console program called MbTest. In the Solution Explorer, right-click "Reference" and click Add Reference. Add a project reference to System.directoryServices on the ".NET" tab. Add a reference to "Microsoft CDO for Exchange Management" on the COM tab. Replace the code in the class1.cs to the following code: use system;

USING CDOEXM;

Using System.directoryServices;

Namespace Mbtest

{

Class class1

{

[Stathread]

Static void main (string [] args)

{

// Todo: Change these items to value 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 of the main function so that they contain the appropriate values ​​for your domain. Compile this project and run the program. Start the "Active Directory User and Computer" management unit in the Microsoft Management Console (MMC), confirm that new accounts have been created in the domain. You will see this new user in the "User" container. To check if this user enabled mailbox, check if the "Exchange" tab appears in the user's properties, and whether the user lists a mailbox store for the user on the "Exchange General" tab.

Back to top

Code description

Creating a new DirectoryEntry This code demonstrates how to bind to the container (in this case "user" container), and how to create a new user in this container. Don't forget to express the "CN =" item of the new user name:

Container = New DirectoryEntry ("LDAP: // CN = Users," DEFAULTNC);

User = container.children.add ("cn =" fullname, "user");

Back to top

Set attributes on the new user

Assign a value for SamaccountName. This is a required attribute; if you do not specify a value, you will not create a user account. Because you have provided the required properties, you want to call commitchanges to save the new user to the directory. Call IADS :: SetPassword to set your password. This must be done after calling commitchange. Enable users by modifying the useeraccountControl property: 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 top

Create a new mailbox

To get an ImailboxStore interface, force DirectoryEntry.nativeOBject to convert DirectoryEntry.nativeObject to this type. If cdoexm is installed on your computer, this forced conversion will not be successful at runtime. Call the CreateMailBox method and pass a valid distinguished name to a mailbox store in your Exchange organization. Call the CommTChanges on DirectoryEntry to save this new email: // Obtain the imividboxstore Interface, Create the mailbox, and commit the change the change ..mailbox = (iMailboxstore) user.nativeObject;

Mailbox.createmailbox (Homemdb);

User.commitchanges ();

Back to top

Troubleshooting

You must have appropriate permissions in the domain to create users and email. Typically, you must create a user enabled in a Windows 2000-based domain, you must be a member of the Windows 2000 domain administrator group. If this code is run on a computer based on Exchange 2000 Server, you must install the Exchange 2000 System Management Tool on this computer. If you don't do this, CDOEXM will not be available, and to force the imaiLboxStore will result in return to 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 when you call iMailboxStore.createMailBox, make sure that the parameters you pass to this method are not a valid email store in your organization. If not, you will receive an error message similar to the following:

An Unhandled Exception of Type 'System.Runtime.InteropServices.comException' occurred in mbtest.exe

Additional Information: There is no claim object on the server.

Back to top

Refer to more information about System.DirectoryServices namespace, please visit the following Microsoft Web site:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemDirectoryServices.asp For more information on cdoexm, please visit the Microsoft Web site below:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wss/wss/_esdk_reference_cdoexm.asp

Back to top

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 Last Updated: 2004-6-17 (2.1 Keywords: kbdswadsi2003swept kbdswadsi2003swept kbhowtomaster KB313114 KBAUDDEVELOPER

转载请注明原文地址:https://www.9cbs.com/read-108909.html

New Post(0)