Article ID: 171429
Article Last Modified on 5/9/2005
/**********************************************************/
// MSGFILTER.CPP
// -------------
// This program demonstrates how the CDO (1.1, 1.2, 1.21)
// MessageFilter works via VC++. This sample requires VC++
// version 5.0 or higher.
/**********************************************************/
// TO DO: If you are using CDO (1.1) unremark the next line:
// #import <olemsg32.dll> no_namespace
// TO DO: If you are using CDO (1.2, 1.21) unremark the next line:
// #import <cdo.dll> no_namespace
#import <olemsg32.dll> no_namespace
#include <assert.h>
#include <stdio.h>
#include <tchar.h>
void dump_com_error(_com_error &e)
{
_tprintf(_T("Oops - hit an error!\n"));
_tprintf(_T("\a\tCode = %08lx\n"), e.Error());
_tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
_tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
_tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}
// If this is placed in the scope of the smart pointers, they must be
// explicitly Release(d) before CoUninitialize() is called. If any
// reference count is non-zero, a protection fault will occur.
struct StartOle {
StartOle() { CoInitialize(NULL); }
~StartOle() { CoUninitialize(); }
} _inst_StartOle;
void main()
{
try
{
// Uncomment one of the next two lines of code as appropriate:
// Create a MAPI.Session pointer with CDO (1.1)
// SessionPtr pSession("MAPI.Session");
// or Create a MAPI.Session pointer with CDO (1.2, 1.21)
// _SessionPtr pSession("MAPI.Session");
// Logon using the specified profile
pSession->Logon("YourValidProfileNameHere");
// Create pointer to the Inbox Folder
FolderPtr pFolder = pSession->Inbox;
// Create pointer to the Messages Collection
MessagesPtr pMessages = pFolder->Messages;
// Get the first Message object before filter is applied
MessagePtr pAMessage = pMessages->GetFirst();
// Process contents of Folder
while (pAMessage != NULL)
{
// Display Properties
MessageBoxW (NULL,pAMessage->Subject.bstrVal,NULL,MB_OK);
// Get next Message
pAMessage = NULL;
pAMessage = pMessages->GetNext();
}
// Destroy the reference to the to the Inbox, Messages
// Collection, and Message then reaquire them
pFolder = NULL;
pMessages = NULL;
pAMessage = NULL;
// Aquire pointer to the Inbox Folder
pFolder = pSession->Inbox;
// Aquire pointer to the Messages Collection
pMessages = pFolder->Messages;
// Acquire pointer and set properties of the MessageFilter
MessageFilterPtr pMsgFilt = pMessages->Filter;
pMsgFilt->Unread = (bool)TRUE;
// Get the first Message object after filter is applied
pAMessage = pMessages->GetFirst();
// Process contents of Folder
while (pAMessage != NULL)
{
// Display Properties
MessageBoxW (NULL,pAMessage->Subject.bstrVal,NULL,MB_OK);
// Get next Message
pAMessage = NULL;
pAMessage = pMessages->GetNext();
}
// Destroy the reference to the Inbox, Messages Collection,
// MessageFilter, and Message then reaquire them
pFolder = NULL;
pMessages = NULL;
pMsgFilt = NULL;
pAMessage = NULL;
// GetFirst() without a filter in place
// Aquire pointer to the Inbox Folder
pFolder = pSession->Inbox;
// Aquire pointer to the Messages Collection
pMessages = pFolder->Messages;
// Get the first Message object after filter is unapplied
pAMessage = pMessages->GetFirst();
// Process contents of Folder
while (pAMessage != NULL)
{
// Display Properties
MessageBoxW (NULL,pAMessage->Subject.bstrVal,NULL,MB_OK);
// Get next Message
pAMessage = NULL;
pAMessage = pMessages->GetNext();
}
// Logoff of the MAPI Session
pSession->Logoff();
}
catch (_com_error &e)
{
dump_com_error(e);
}
}
Keywords: kbhowto kbmsg kbcode KB171429