HOWTO: Get Message Store Information on Mailboxes Programmatically Using Visual C++

ID: Q198752


The information in this article applies to:


SUMMARY

Retrieving information about mailboxes like the size and the number of messages is a task that is common in maintenance applications. This article contains a Microsoft Visual C++ code sample that demonstrates how to view that type of information. It uses properties on the root folder to determine overall size. This same logic can be applied to any folder within the mailbox.


MORE INFORMATION

The following code needs to be modified to include the organization and site for the user's messaging environment. It is a console application and requires the following libraries:

Sample Code


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

   //  create server DN
   sprintf(pszServerDN,"/o=org/ou=site/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 );
            
            //Create Information Store DN
            sprintf(pszServerDN,
                       "/o=org/ou=site/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);
            }
            //  *********  Place Mailbox Processing Here *********
            LPSPropValue lppProp = NULL;
            if (FAILED(hr = HrGetOneProp(lpUserMDB, PR_MESSAGE_SIZE, 
                                         &lppProp)))
            {
               char szError[200];
               sprintf(szError, "HrGetOneProp error %x",hr);
               MessageBox(0L,szError,"Error",MB_OK);
            }
            else
            {
               char szInfo[200];
               sprintf(szInfo,"Message Size = %d",lppProp->Value);
               MessageBox(0L,szInfo,"",MB_OK);
            }
            if (FAILED(hr = HrGetOneProp(lpUserMDB, PR_CONTENT_COUNT, 
                                         &lppProp)))
            {
               char szError[200];
               sprintf(szError, "HrGetOneProp error %x",hr);
               MessageBox(0L,szError,"Error",MB_OK);
            }
            else
            {
               char szInfo[200];
               sprintf(szInfo,"Total Message Count = %d",lppProp->Value);
               MessageBox(0L,szInfo,"",MB_OK);
            }
         }
      }
   }
   
   if(lpRows)
   {
      FreeProws(lpRows);
   }

   if(lpMailBoxTable)
   {
      lpMailBoxTable->Release();
   }

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

Additional query words: kbMsg kbEDK500 kbEDK550


Keywords          : kbMsg kbEDK500 kbEDK550 
Version           : WINDOWS:5.0,5.5
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: February 2, 1999