Microsoft KB Archive/195545

= How To Use CDO 1.x/VC++ to Get Properties on Received Messages =

Article ID: 195545

Article Last Modified on 8/30/2004

-

APPLIES TO


 * Microsoft Collaboration Data Objects 1.1
 * Microsoft Collaboration Data Objects 1.2
 * Microsoft Collaboration Data Objects 1.21

-



This article was previously published under Q195545



SUMMARY
This article demonstrates how to use Collaboration Data Objects (CDO 1.x) from Visual C++ to retrieve properties from an e-mail message that you have received as a recipient.



MORE INFORMATION
The following code opens the first message it finds in the Inbox then retrieves the Senders Name and Message Body. These fields are selected merely for demonstration; they are not the only fields that you can retrieve.

NOTE: This code references Cdo.dll, which is CDO 1.2x. If you are using CDO 1.1, you need to change the #import as follows: #import "olemsg32.dll" no_namespace

Sample Code
// Import the lib to generate the SmartPointers. #import "cdo.dll" no_namespace

#include   #include 

// 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); }  }

