HOWTO: Retrieve Messages Based on a Particular Criteria

ID: Q200181


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

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.


MORE INFORMATION

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;

} 


REFERENCES

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

Q177622 HOWTO: How to Create a Restriction

Additional query words: kbDSupport kbXchge kbMsg kbEDK kbMAPI100

Keywords : kbEDK kbXchge kbMsg kbMAPI100
Version : WINDOWS:1.0,5.0,5.5
Platform : WINDOWS
Issue type : kbhowto


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