HOWTO: Opening Mailboxes with Privileged Access

ID: Q194627


The information in this article applies to:
  • Extended Messaging Application Programming Interface (MAPI), version 1.0
  • Exchange Development Kit (EDK), versions 5.0, 5.5


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 <stdio.h>
    #include <edk.h>

    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))
       {
          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 );

                // 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(FAILED(hr = HrMailboxLogon(lpMAPISession,
                                   lpMDB,pszServerDN,lpspv->Value.lpszA,
                                   &lpUserMDB)))
                {
                    MessageBox(0L,"Mailbox Not Available","Error",MB_OK);
                    return MAPI_E_NOT_FOUND;
                }
                else
                {
                    printf("Opened %s \n",lpspv->Value.lpszA);
                }
                // TO DO: ****** Place Mailbox Processing Here.
             }
          }
       }

       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 :
Version : WINDOWS:1.0,5.0,5.5
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 4, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.