#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);
}