Microsoft KB Archive/194627

= How To Open Mailboxes with Privileged Access =

Article ID: 194627

Article Last Modified on 8/25/2005

-

APPLIES TO


 * Microsoft Messaging Application Programming Interface
 * Microsoft Exchange Development Kit 5.5
 * Microsoft Exchange Server 2003 Software Development Kit

-



This article was previously published under Q194627



SUMMARY
Applications that are doing statistics or monitoring of mailboxes need to access all the mailboxes. You can accomplish this by logging into the private information store with a privileged account, looping through the list of mailboxes, and open the mailboxes with the HrMailboxLogon function.



MORE INFORMATION
The following code shows how to open all of the mailboxes on a server. It requires the Windows NT account you use to launch the application to have administrative privileges on the Exchange Server Directory.

Here is the list of additional libraries used to compile this code. (You should add these to the libraries required for a Win32 console application.)


 * version.lib
 * Exchsdk.lib
 * Mapi32.lib
 * Edkutils.lib
 * Edkmapi.lib
 * Msvcrt.lib

There are some items within the code marked with "TO DO:" that you need to change to use the organization and site information for the server you access. It is also possible for you to change the code to accept the server Distinguished Name (DN) from the prompt. #include    #include 

HRESULT OpenMailbox(LPMAPISESSION lpMAPISession,                       LPSTR pszExchangeServerName);

void main {      HRESULT hr = S_OK;

hr = MAPIInitialize(NULL); if(FAILED(hr)) {         printf("Failed to initialize MAPI\n"); }

char pszExchangeServerName[500]; LPMAPISESSION lpSess = NULL; LPMDB  lpMDB = NULL;

// Get the Exchange Server name from the user. //       printf("n\nPlease enter the name of your Exchange System ?  "); gets(pszExchangeServerName); printf("\n\n");

// TO DO: Need to logon using a profile for the service account or      //        an Exchange Admin. hr = MAPILogonEx(0, "", NULL,              MAPI_LOGON_UI | MAPI_NEW_SESSION |  MAPI_EXPLICIT_PROFILE,               &lpSess); if (FAILED(hr)) {         MessageBox(NULL,"MAPI Logon failed",NULL,MB_OK); }

if(SUCCEEDED(hr)&& lpSess) {         printf("Created MAPI session\n"); hr = OpenMailbox(lpSess, pszExchangeServerName);

if(FAILED(hr)) printf("Failed to Run\n"); else printf("Opened users mailboxes\n");

}

char ch; printf("\nHit a key to exit"); ch = getchar;

if(lpSess) lpSess->Release;

}

HRESULT OpenMailbox(LPMAPISESSION lpMAPISession, LPSTR pszServerName) {      HRESULT      hr            = S_OK; LPMAPITABLE lpMailBoxTable = NULL; LPSRowSet  lpRows         = NULL; LPENTRYID  lpMsgStoreID   = NULL; ULONG     cbMsgStoreID   = 0; LPMDB     lpMDB         = NULL; LPMDB     lpUserMDB      = NULL; LPMAPIFOLDER lpFolder     = NULL; LPEXCHANGEMANAGESTORE lpIManageStore = NULL; char pszServerDN[500];

if (FAILED(hr = HrOpenExchangePrivateStore(lpMAPISession, &lpMDB))) {         MessageBox(0L,"Message Store Not Available","Error",MB_OK); return MAPI_E_NOT_FOUND; }

if (FAILED(hr = lpMDB->QueryInterface(IID_IExchangeManageStore,                               (void **) &lpIManageStore))) {         MessageBox(0L,"QueryInterace Failed","Error",MB_OK); return MAPI_E_NOT_FOUND; }

// TO DO: Create server DN. Replace "myorgname" and "mysitename" //       with appropriate organization and site name. sprintf(pszServerDN,"/o=myorgname/ou=mysitename/cn=servers/cn=%s",              pszServerName);

if (FAILED(hr = lpIManageStore->GetMailboxTable(pszServerDN,                                       &lpMailBoxTable,0))) {         MessageBox(0L,"Mailbox Table Not Available","Error",MB_OK); return MAPI_E_NOT_FOUND; }

// Get a list of Mailboxes taking up resources. hr = HrQueryAllRows(lpMailBoxTable, NULL, NULL, NULL, 0, &lpRows);

if(SUCCEEDED(hr)) {            // TO DO: Create Information Store DN. Replace "myorgname" //       and "mysitename" with appropriate organization //       and site name. sprintf(pszServerDN,                    "/o=myorgname/ou=mysitename/cn=servers/cn=%s%s",                     pszServerName,"/cn=Microsoft Private MDB");

if (lpRows->cRows > 0) {               for (UINT i=0; i < lpRows->cRows; i++) {                  LPSPropValue lpspv = PpropFindProp(                                       lpRows->aRow[i].lpProps,                                       lpRows->aRow[i].cValues,                                       PR_EMAIL_ADDRESS );

if(FAILED(hr = HrMailboxLogon(lpMAPISession,                                     lpMDB,pszServerDN,lpspv->Value.lpszA,                                      &lpUserMDB))) {                      MessageBox(0L,"Mailbox Not Available","Error",MB_OK); continue; }                  else {                      printf("Opened %s \n",lpspv->Value.lpszA); }                  // TO DO: ****** Place Mailbox Processing Here. HrMailboxLogoff(&lpUseMDB); }            }          }

if(lpRows) {         FreeProws(lpRows); }

if(lpMailBoxTable) {         lpMailBoxTable->Release; }

if (FAILED(hr)) return MAPI_E_NOT_FOUND; else return S_OK; }

Additional query words: kbDSupport kbMAPI100 kbMsg kbEDK500 kbEDK550

Keywords: kbhowto kbcode KB194627

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.