STDMETHODIMP ZybraxiSoft::CMailbox::GetServerNameFromProfile(TSTRING & sServerName) { HRESULT hr = S_OK; LPSERVICEADMIN lpSvcAdmin = NULL; LPPROFSECT lpProfSect = NULL; LPSPropValue lpSPropServer = NULL; // Get the services admin // See https://msdn.microsoft.com/en-us/library/cc765886(v=office.15).aspx if (SUCCEEDED(hr = m_lpSession->AdminServices( NULL, // Reserved - must be zero &lpSvcAdmin))) // pointer to pointer to the service admin obj { // Open the profile section // see https://msdn.microsoft.com/en-us/library/office/cc839895.aspx if (SUCCEEDED(hr = lpSvcAdmin->OpenProfileSection( (LPMAPIUID)pbGlobalProfileSectionGuid, // lpUID NULL, // lpInterface; NULL - IProfSect NULL, // ulFlags - no flags set &lpProfSect))) { // Get the property value for the server if (SUCCEEDED(hr = HrGetOneProp( lpProfSect, // pmp; pointer to IMAPIProp interface PR_PROFILE_HOME_SERVER, // ulPropTag &lpSPropServer))) // ppprop; pointer to a pointer to an SPropValue { // Now the tricky part; profile parts are all ASCII so we need to know // whether to convert it or not. This is going to depend on whether our // TSTRING resolves to wstring or simply string wstring* wsPtr = NULL; wsPtr = dynamic_cast<wstring*>(&sServerName); if (wsPtr) // Cast succeeded, we're dealing with wstring { std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; sServerName = converter.from_bytes(lpSPropServer->Value.lpszA); } else { sServerName = TSTRING(lpSPropServer->Value.LPSZ); } } else { ERROR_MSG_W_HR("Unable to get the PR_PROFILE_HOME_SERVER", hr); goto CLEANUP; } } else { ERROR_MSG_W_HR("Unable to open the profile section", hr); goto CLEANUP; } } else { ERROR_MSG_W_HR("Unable to get the services admin", hr); } CLEANUP: if (lpSPropServer) { MAPIFreeBuffer(lpSPropServer); lpSPropServer = NULL; } if (lpProfSect) { lpProfSect->Release(); lpProfSect = NULL; } if (lpSvcAdmin) { lpSvcAdmin->Release(); lpSvcAdmin = NULL; } return hr; }
HRESULT ConfigMsgService(){ HRESULT hr = 0; LPPROFADMIN lpProfAdmin = NULL; LPSERVICEADMIN lpServiceAdmin = NULL; LPMAPITABLE lpMapiTable = NULL; SRestriction sres; // Restriction structure. SPropValue SvcProps; // Property structure for restriction. LPSRowSet lpSvcRows = NULL; // Rowset to hold results of table query. LPSTR szServer = "155.35.79.109"; LPSTR szMailbox = "InputedBox"; SPropValue rgval[2]; // Property structure to hold values we want to set. enum { iSvcName, iSvcUID, cptaSvc }; SizedSPropTagArray(cptaSvc, sptCols) = { cptaSvc, PR_SERVICE_NAME, PR_SERVICE_UID }; do{ // if not profile, create profile. // else use exist profile DEFINE_IF_HR_NT_OK_BREAK(MAPIAdminProfiles(0, &lpProfAdmin)); LPTSTR strProfileName = L"lhytest"; LPTSTR strProfilePsw = L"123.com"; hr = lpProfAdmin->CreateProfile(strProfileName, NULL, NULL, 0); if (hr == MAPI_E_NO_ACCESS){ // profile exist; break; } else if (hr == S_OK){ DEFINE_IF_HR_NT_OK_BREAK(lpProfAdmin->AdminServices(strProfileName, NULL, NULL, 0, &lpServiceAdmin)); DEFINE_IF_HR_NT_OK_BREAK(lpServiceAdmin->CreateMsgService((LPTSTR)"MSEMS", NULL, 0, 0)); // todo config MsgService. hr = lpServiceAdmin->GetMsgServiceTable(0, &lpMapiTable); DEFINE_IF_HR_NT_OK_BREAK(hr); sres.rt = RES_CONTENT; sres.res.resContent.ulFuzzyLevel = FL_FULLSTRING; sres.res.resContent.ulPropTag = PR_SERVICE_NAME; sres.res.resContent.lpProp = &SvcProps; SvcProps.ulPropTag = PR_SERVICE_NAME; SvcProps.Value.lpszA = "MSEMS"; // Query the table to obtain the entry for the newly created message service. if (FAILED(hr = HrQueryAllRows(lpMapiTable, (LPSPropTagArray)&sptCols, &sres, NULL, 0, &lpSvcRows))) { break; } // Set up a SPropValue array for the properties that you have to configure. // First, the server name. ZeroMemory(&rgval[1], sizeof(SPropValue)); rgval[1].ulPropTag = PR_PROFILE_UNRESOLVED_SERVER; rgval[1].Value.lpszA = szServer; // Next, the mailbox name. ZeroMemory(&rgval[0], sizeof(SPropValue)); rgval[0].ulPropTag = PR_PROFILE_UNRESOLVED_NAME; rgval[0].Value.lpszA = szMailbox; // Configure the message service by using the previous properties. if (FAILED(hr = lpServiceAdmin->ConfigureMsgService( (LPMAPIUID)lpSvcRows->aRow->lpProps[iSvcUID].Value.bin.lpb, // Entry ID of service to configure. NULL, // Handle to parent window. 0, // Flags. 2, // Number of properties we are setting. rgval))) // Pointer to SPropValue array. { break; } } else { break; } } while (0); if (lpSvcRows != NULL){ FreeProws(lpSvcRows); lpSvcRows = NULL; } if (lpMapiTable != NULL){ lpMapiTable->Release(); lpMapiTable = NULL; } if (lpServiceAdmin != NULL){ lpServiceAdmin->Release(); lpServiceAdmin = NULL; } if (lpProfAdmin != NULL){ lpProfAdmin->Release(); lpProfAdmin = NULL; } return hr; }