Article ID: 195545
Article Last Modified on 8/30/2004
#import "olemsg32.dll" no_namespace
// Import the lib to generate the SmartPointers.
#import "cdo.dll" no_namespace
#include <stdio.h>
#include <tchar.h>
// Setup Error Handling.
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);
}
// Initialize COM.
// 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 occurs.
struct StartOle {
StartOle() { CoInitialize(NULL); }
~StartOle() { CoUninitialize(); }
} _inst_StartOle;
void main(int argc, char *argv[])
{
try
{
// Create a Session and Logon.
_SessionPtr pSession("MAPI.Session");
pSession->Logon();
// Get a Message.
FolderPtr pFolder = pSession->Inbox;
MessagesPtr pMessages = pFolder->Messages;
MessagePtr pMessage = pMessages->GetFirst();
if (pMessage!=NULL)
{
char rgch[256];
AddressEntryPtr pAESender = pMessage->Sender;
// Convert UNICODE return from multi to single byte value.
WideCharToMultiByte(CP_ACP,
0,
pAESender->Name.bstrVal,
-1,
rgch,
sizeof(rgch),
NULL,
NULL);
// Display the Senders Name.
MessageBox(NULL, rgch, "Sent By", MB_OK);
// Display message text.
try
{
// Wrap the following code into a try/catch because...
// If the PR_BODY field is not present in the underlying
// MAPI Message, the WideCharToMultiByte function
// yields a MAPI_E_NOT_FOUND when trying to access the CDO
// Message->Text property. You will generally want to
// follow this approach for each property accessed.
//
char strMessageText[1024];
// Convert UNICODE return from multi to single byte value.
WideCharToMultiByte(CP_ACP,
0,
pMessage->Text.bstrVal,
-1,
strMessageText,
sizeof(strMessageText),
NULL,
NULL);
// Display the Message Body.
MessageBox(NULL, strMessageText, "Body", MB_OK);
}
catch (_com_error &e)
{
// Put appropriate error condition code here.
}
}
else
MessageBox(NULL, "No messages found", "Inbox", MB_OK);
// Logoff the Active Messaging MAPI Session.
pSession->Logoff();
}
catch (_com_error &e)
{
dump_com_error(e);
}
}
171440 Where to Acquire the Collaboration Data Objects Libraries
Keywords: kbhowto kbmsg kbfaq KB195545