How to programmatically read additional mailboxes from an existing mail profile

Reading additional mailboxes from a given e-mail profile is much simpler than adding them. After retrieving all installed providers for the MSEMS service, just loop thru all providers and search for the ones with PR_PROFILE_TYPE set to the value 2.

This is documented here:

For each 'delegate store' simply read the following properties (which are later required if you want to add this mailbox to a different/new.... e-mail profile):


Here's a code snippet which shows on how to enumerate the installed providers for the MSEMS service:

This code is sample code. these samples are provided "as is" without warranty of any kind. Microsoft further disclaims all implied warranties including without limitation any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the samples remains with you. in no event shall Microsoft or its suppliers be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the samples, even if Microsoft has been advised of the possibility of such damages. Because some states do not allow the exclusion or limitation of liability for consequential or incidental damages, the above limitation may not apply to you.


 // Enumeration for convenience.
 enum {iDispName, iSvcName, iSvcUID, cptaSvc};

 // This structure tells HrQueryAllRows what columns we want returned.
 SizedSPropTagArray(cptaSvc,sptCols) = { cptaSvc,
       PR_SERVICE_UID };
 // This structure tells HrQueryAllRows what columns we want returned.
 // Information when reading the installed provider services
 static SizedSPropTagArray(3, sptProvt) = {3, {  PR_DISPLAY_NAME,
       PR_PROVIDER_UID} };

 // Get an IProfAdmin interface.
 hRes = MAPIAdminProfiles(0, &lpProfAdmin);// Pointer to new IProfAdmin
 if (FAILED(hRes)) goto error_handler;
 // Get an IMsgServiceAdmin interface off of the IProfAdmin interface.
 hRes = lpProfAdmin->AdminServices(lpszProfile, "", NULL, 0, &lpSvcAdmin);// Pointer to new IMsgServiceAdmin.
 if (FAILED(hRes))
  printf("Can't open Mail profile %s\n", lpszProfile);
  goto error_handler;
 // We now need to get the entry id for the Exchange service.
 // First, we get the Message service table.
 hRes = lpSvcAdmin->GetMsgServiceTable(0, &lpMsgSvcTable);// Pointer to table
 if (FAILED(hRes)) goto error_handler;
 // Set up restriction to query table.
 sres.rt = RES_PROPERTY;
 sres.res.resProperty.relop = RELOP_EQ;
 sres.res.resProperty.ulPropTag = PR_SERVICE_NAME;
 sres.res.resProperty.lpProp = &SvcProps;

 SvcProps.ulPropTag = PR_SERVICE_NAME;
 SvcProps.Value.lpszA = "MSEMS";

 // Query the table to get the entry for the Exchange message service.
 hRes = HrQueryAllRows(lpMsgSvcTable, (LPSPropTagArray)&sptCols, &sres, NULL, 0, &lpSvcRows);
 if (FAILED(hRes)) goto error_handler;

 // Test if exchange service was found
 if (lpSvcRows->cRows == 0)
  printf("MSEMS Service not found in Mail profile %s\n", lpszProfile);
  goto error_handler;

 // Get a provider admin pointer.
 hRes = lpSvcAdmin->AdminProviders((LPMAPIUID)lpSvcRows->aRow->lpProps[iSvcUID].Value.bin.lpb, 0, &lpProvAdmin);
 if (FAILED(hRes)) goto error_handler;
 hRes = lpProvAdmin->GetProviderTable(NULL, &pMTProviders);
 if (FAILED(hRes)) goto error_handler;
 hRes = HrQueryAllRows(pMTProviders, (LPSPropTagArray)&sptProvt, NULL, NULL, 0, &pProvRows);
 if (FAILED(hRes)) goto error_handler;


At this point, the rowset pProvRows contains all installed provider services for the MSEMS service and you can loop thru and read the properties.

Comments (1)

  1. How to programmatically read additional mailboxes from an existing mail profile
    thank you

Skip to main content