Microsoft KB Archive/293339

= How to create a mailbox-enabled user with CDOEXM in Visual C++ =

Article ID: 293339

Article Last Modified on 2/22/2007

-

APPLIES TO


 * Microsoft Exchange 2000 Server Standard Edition
 * Collaboration Data Objects for Exchange 2000
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition

-



This article was previously published under Q293339



SUMMARY
This article contains sample Microsoft Visual C++ code that demonstrates how to create a mailbox-enabled user by using CDO for Exchange Management (CDOEXM).



MORE INFORMATION
 In Visual C++, create a new console project. Add a new source file to the project.  Paste the following code in the source file:
 * 1) import &quot;d:\Program Files\Common Files\Microsoft Shared\CDO\cdoex.dll&quot; no_namespace raw_interfaces_only rename(&quot;Folder&quot;,&quot;CDOEXFolder&quot;)
 * 2) import &quot;d:\Program Files\Exchsrvr\BIN\cdoexm.dll&quot; no_namespace raw_interfaces_only
 * 3) import &quot;d:\Program Files\Common Files\System\ado\msado15.dll&quot; no_namespace rename(&quot;EOF&quot;,&quot;adoEOF&quot;), named_guids

int main {   HRESULT hr; hr = CoInitialize(NULL); {        IPersonPtr pPerson(__uuidof(Person)); IMailboxStorePtr pMailbox; IDataSourcePtr pDataSource; FieldsPtr pFields; FieldPtr pField;

/*       strServerName   - This is the server name. For example: &quot;MyServer6&quot; */      _bstr_t strServerName = &quot;servername&quot;;

/*       strDomainName   - This is the server's domain. For example: &quot;DC=MYDOMAIN3,DC=microsoft,DC=com&quot; */      _bstr_t strDomainName = &quot;dc=mydomain,dc=extest,dc=microsoft,dc=com&quot;; /*       strLoginName - This is the login name. Note that this is the same as the e-mail name, but in some cases can be different. For example: &quot;lname: */      _bstr_t strLoginName = &quot;EmailName&quot;; /*       strExchangeOrg  - This is the Exchange organization that houses the mailbox store. For example: &quot;First Organization&quot; */      _bstr_t strExchangeOrg = &quot;First Organization&quot;;

/*       strAdminGroup   - This is the Exchange administrative group name. For example: &quot;First Administrative Group&quot; */      _bstr_t strAdminGroup = &quot;First Administrative Group&quot;; /*     strStorageGroup - This is the storage group for the mailbox store. For example: &quot;First Storage Group&quot; */      _bstr_t strStorageGroup = &quot;First Storage Group&quot;;

/*     strStoreName    - This is the mailbox store name. For example: &quot;Mailbox Store (SERVERNAME)&quot; */      _bstr_t strStoreName = &quot;Mailbox Store (SERVERNAME)&quot;; /*     strEmailName    - This is the user's e-mail name. For example: &quot;FirsnameLastName&quot; */      _bstr_t strEmailName = &quot;EmailPerson&quot;;

/*     strDNSSuffix - the server's DNS suffix. For example &quot;MYDOMAIN3.microsoft.com */      _bstr_t strDNSSuffix = &quot; MYDOMAIN3.microsoft.com &quot;; /*       strFirstName    - This is the user's first name. For example: &quot;Firstname&quot; */      _bstr_t strFirstName = &quot;Email&quot;; /*     strLastName     - This is the user's last name. For example: &quot;Lastname&quot; */      _bstr_t strLastName = &quot;Person&quot;; /*       strPassword     - This is the user's password. For example: &quot;password&quot; */      _bstr_t strPassword = &quot;password&quot;; /*       strUserAccountControl     - This is the type of account. For example: &quot;512&quot; */      _bstr_t strUserAccountControl = &quot;512&quot;; /*     _bstr_t strURL = &quot;LDAP://servername/CN=EmailPerson,CN=users,dc=mydomain,dc=extest,dc=microsoft,dc=com&quot;; */      _bstr_t strURL  = &quot;LDAP://&quot; + strServerName + &quot;/CN=&quot; + strEmailName + &quot;,CN=users,&quot; + strDomainName;

/*       _bstr_t strCreateString = &quot;LDAP://servername/CN=Mailbox Store (SERVERNAME),&quot; &quot;CN=First Storage Group, CN=InformationStore, CN=servername,&quot; &quot;CN=Servers, CN=First Administrative Group, CN=Administrative &quot; &quot;Groups,CN=First Organization,CN=Microsoft Exchange, CN=Services,&quot; &quot;CN=Configuration, dc=mydomain, dc=extest, dc=microsoft, dc=com&quot;; */      _bstr_t strCreateString = &quot;LDAP://&quot; + strServerName + &quot;/CN=&quot; + strStoreName + &quot;,CN=&quot; + strStorageGroup + &quot;,CN=InformationStore,CN=&quot; + strServerName + &quot;,CN=Servers,CN=&quot; + strAdminGroup + &quot;,&quot; + &quot;CN=Administrative Groups,CN=&quot; + strExchangeOrg + &quot;,&quot; + &quot;CN=Microsoft Exchange, CN=Services,&quot; + &quot;CN=Configuration,&quot; + strDomainName; //Get the fields from the person. hr = pPerson->get_Fields(&pFields); //Set the first and last name. hr = pPerson->put_FirstName((BSTR)strFirstName); hr = pPerson->put_LastName((BSTR)strLastName); //Set userPrincipalName. _bstr_t strUPN = strLoginName + &quot;@&quot; + strDNSSuffix; pField = pFields->GetItem(&quot;userPrincipalName&quot;); pField->PutValue(strUPN); //Set userAccountControl. pField = pFields->GetItem(&quot;userAccountControl&quot;); pField->PutValue(strUserAccountControl); //Set userPassword. pField = pFields->GetItem(&quot;userPassword&quot;); pField->PutValue(strPassword); //Save the changes. hr = pFields->Update; //Get the DataSource and create the user. hr = pPerson->get_DataSource(&pDataSource); hr = pDataSource->SaveTo(            (BSTR)strURL,              NULL,              adModeReadWrite,              adCreateNonCollection,              (RecordOpenOptionsEnum) NULL,              bstr_t,              bstr_t); //Get MailBoxStore and mailbox-enable the user. hr = pPerson->QueryInterface(            __uuidof(IMailboxStore),            (void**)&pMailbox); hr = pMailbox->CreateMailbox((BSTR)strCreateString); //Save the DataSource. hr = pDataSource->Save; }

CoUninitialize; return(0);

} NOTE: You must change the path of the imported .dlls according to the location of the .dll on your computer, and change the placeholders such as the server name, domain name, and so on to correspond with your Exchange organization.

 Compile and run the application.

