Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

This article was previously published under Q293339

↑ Back to the top


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).

↑ Back to the top


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:
    #import "d:\Program Files\Common Files\Microsoft Shared\CDO\cdoex.dll" no_namespace raw_interfaces_only rename("Folder","CDOEXFolder") 
    #import "d:\Program Files\Exchsrvr\BIN\cdoexm.dll" no_namespace raw_interfaces_only
    #import "d:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF"), 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: "MyServer6"
        */ 
          _bstr_t strServerName = "servername";
    
        /*
            strDomainName   - This is the server's domain.
            For example: "DC=MYDOMAIN3,DC=microsoft,DC=com"
        */ 
          _bstr_t strDomainName = "dc=mydomain,dc=extest,dc=microsoft,dc=com";
        
        /*
            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: "lname:
        */ 
          _bstr_t strLoginName = "EmailName";
          
        /*
            strExchangeOrg  - This is the Exchange organization that houses the mailbox store. 
            For example: "First Organization"
        */ 
          _bstr_t strExchangeOrg = "First Organization";
    
        /*
            strAdminGroup   - This is the Exchange administrative group name.
            For example: "First Administrative Group"
        */ 
          _bstr_t strAdminGroup = "First Administrative Group";
          
        /*
          strStorageGroup - This is the storage group for the mailbox store.
            For example: "First Storage Group"
        */ 
          _bstr_t strStorageGroup = "First Storage Group";
    
        /*
          strStoreName    - This is the mailbox store name.
          For example: "Mailbox Store (SERVERNAME)"
        */ 
          _bstr_t strStoreName = "Mailbox Store (SERVERNAME)";
          
        /*
          strEmailName    - This is the user's e-mail name.
          For example: "FirsnameLastName"
        */ 
          _bstr_t strEmailName = "EmailPerson";
    
        /*
          strDNSSuffix - the server's DNS suffix.
          For example "MYDOMAIN3.microsoft.com
        */ 
          _bstr_t strDNSSuffix = " MYDOMAIN3.microsoft.com ";
          
        /*
            strFirstName    - This is the user's first name.
            For example: "Firstname"
        */ 
          _bstr_t strFirstName = "Email";
          
        /*
          strLastName     - This is the user's last name.
          For example: "Lastname"
        */ 
          _bstr_t strLastName = "Person";
          
        /*
            strPassword     - This is the user's password.
            For example: "password"
        */ 
          _bstr_t strPassword = "password";
          
        /*
            strUserAccountControl     - This is the type of account.
            For example: "512"
        */ 
          _bstr_t strUserAccountControl = "512";
          
        /*
          _bstr_t strURL = "LDAP://servername/CN=EmailPerson,CN=users,dc=mydomain,dc=extest,dc=microsoft,dc=com";
        */ 
          _bstr_t strURL  = "LDAP://" + strServerName + "/CN=" + strEmailName +
                            ",CN=users," + strDomainName;
    
          /*
            _bstr_t strCreateString = 
            "LDAP://servername/CN=Mailbox Store (SERVERNAME),"
            "CN=First Storage Group, CN=InformationStore, CN=servername,"
            "CN=Servers, CN=First Administrative Group, CN=Administrative "
            "Groups,CN=First Organization,CN=Microsoft Exchange, CN=Services," 
            "CN=Configuration, dc=mydomain, dc=extest, dc=microsoft, dc=com";
          */ 
          _bstr_t strCreateString = 
              "LDAP://" + strServerName + "/CN=" + strStoreName + ",CN=" +
              strStorageGroup + ",CN=InformationStore,CN=" + strServerName +
              ",CN=Servers,CN=" + strAdminGroup + "," + 
              "CN=Administrative Groups,CN=" + strExchangeOrg + "," +
              "CN=Microsoft Exchange, CN=Services," + 
              "CN=Configuration," + 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 + "@" + strDNSSuffix;
          pField = pFields->GetItem("userPrincipalName");
          pField->PutValue(strUPN);
          
          //Set userAccountControl.
          pField = pFields->GetItem("userAccountControl");
          pField->PutValue(strUserAccountControl);
          
          //Set userPassword.
          pField = pFields->GetItem("userPassword");
          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.

↑ Back to the top


References

For additional information on CDOEXM and its interfaces, see the corresponding topics in Microsoft Developer Network (MSDN) library.

↑ Back to the top


Keywords: KB293339, kbmsg, kbhowto

↑ Back to the top

Article Info
Article ID : 293339
Revision : 7
Created on : 2/22/2007
Published on : 2/22/2007
Exists online : False
Views : 500