HOW TO: Retrieve Messages Based on a Particular Criteria (200181)



The information in this article applies to:

  • Microsoft Extended Messaging Application Programming Interface (MAPI)
  • Microsoft Exchange Development Kit (EDK) 5.0
  • Microsoft Exchange Development Kit (EDK) 5.5

This article was previously published under Q200181

SUMMARY

This step-by-step article describes how to retrieve messages based on a particular criteria.

Electronic mailboxes sometimes receive hundreds of messages daily. It may become necessary to filter the messages by some criteria like subject or date. This article contains sample code that demonstrates one way to perform this task.

back to the top

Sample Code

The following code should be compiled using the following libraries:

  • Mapi32.lib
  • Uuid.lib
  • Version.lib
  • Edkmapi.lib
  • Edkutils.lib
  • Addrlkup.lib
  • Edkguid.lib
  • Rulecls.lib
  • Edkdebug.lib
  • Msvcrt.lib

#include <Windows.h>
#include <edk.h>
#include <edkdebug.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pszCmd, int nCmdShow)
{

   // Load strings, and register window classes.
   LPMAPISESSION    lpSession              = NULL;
   LPMDB            lpStore                = NULL;
   CHAR             szStore[MAX_PATH + 1]  = {0};
   ULONG            cbEIDStore             = 0;
   LPENTRYID        lpEIDStore             = NULL;
   CHAR             szFolder[MAX_PATH + 1] = {0};
   LPMAPIFOLDER     lpFolder               = NULL;
   ULONG            cbEIDFolder            = 0;
   LPENTRYID        lpEIDFolder            = NULL;
   LONG             lStateFlags            = ST_ENABLED;
   HRESULT          hr                     = NULL;
   ULONG            ulFlags                = MAPI_BEST_ACCESS;
   LPSTR            pszStore               = NULL;
   LPSTR            lpszExplicitClass      = "";
   ULONG            ulObjType              = 0;
   LPMAPITABLE      lpTable                = NULL;
   LPSRowSet        lppRows                = NULL;
   SRestriction     sRes;
   SPropValue       spvSubj;



   hr = MAPIInitialize(NULL);
   if (FAILED(hr))
   {

      return 1;

   }
   hr = MAPILogonEx(0,
                 "",
                 NULL,
                 MAPI_LOGON_UI | MAPI_NEW_SESSION |  MAPI_EXPLICIT_PROFILE,
                 &lpSession);
   if (FAILED(hr))
   {
      MessageBox(NULL,"MAPI Logon failed",NULL,MB_OK);
      goto cleanup;
   }
   hr = HrOpenExchangePrivateStore(lpSession, &lpStore);
   if (FAILED(hr))
   {
      MessageBox(NULL,"Message Store Not Opened",NULL,MB_OK);
      goto cleanup;
   }
   strcpy(szFolder, "Top of Information Store\\Inbox");

   hr = HrMAPIFindFolderEx(lpStore,
                        '\\',
                        szFolder,
                        &cbEIDFolder,
                        &lpEIDFolder);
   if (FAILED(hr))
   {
      MessageBox(NULL,"Inbox Not Found",NULL,MB_OK);
      goto cleanup;
   }

   hr = lpStore->OpenEntry(cbEIDFolder,
                     lpEIDFolder,
                     NULL,
                     MAPI_BEST_ACCESS   | MAPI_DEFERRED_ERRORS,
                     &ulObjType,
                     (LPUNKNOWN FAR *)&lpFolder);
   if (FAILED(hr))
   {
      MessageBox(NULL,"Inbox Could Not Be Opened",NULL,MB_OK);
      goto cleanup;
   }
   spvSubj.ulPropTag = PR_SUBJECT;
   spvSubj.Value.lpszA = "Test";
   sRes.rt = RES_CONTENT;
   sRes.res.resContent.ulFuzzyLevel = FL_SUBSTRING | FL_IGNORECASE | FL_LOOSE;
   sRes.res.resContent.ulPropTag = PR_SUBJECT;
   sRes.res.resContent.lpProp = &spvSubj;
   hr = lpFolder->GetContentsTable(MAPI_DEFERRED_ERRORS, &lpTable);
   if (FAILED(hr))
      printf("GetContentsTable Failed\n");
   else
      printf("Table Returned\n");
   hr = HrQueryAllRows(lpTable,NULL,&sRes,NULL,0L,&lppRows);
   if (FAILED(hr))
      printf("HrQueryAllRows Failed\n");
   else
      printf("HrQueryAllRows Succeeded\n");
cleanup:
   if (lppRows)
      FREEPROWS(lppRows);
   if (lpSession)
   {
      lpSession->Logoff(0, 0, 0);
      ULRELEASE(lpSession);
   }
   MAPIUninitialize();
   return 0;

}
back to the top

REFERENCES

For more information about what a restriction is and how to make one, please see the following article in the Microsoft Knowledge Base:

177622 HOWTO: How to Create a Restriction

back to the top

Modification Type:MinorLast Reviewed:8/25/2005
Keywords:kbHOWTOmaster kbMsg KB200181 kbAudDeveloper