HOWTO: Use CDO/VC++ to Get Properties on Received Messages

ID: Q195545

The information in this article applies to:

SUMMARY

This article demonstrates how to use Collaboration Data Objects (CDO) 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 <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);
      }
   }

REFERENCES

For more information on where to acquire the CDO libraries, please see: the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q171440
   TITLE     : Where to Acquire the Collaboration Data Objects Libraries

Additional query words:
Keywords          : kbCDO110 kbCDO120 kbCDO121 kbMsg kbVC kbGrpMsg 
Version           : WINDOWS:1.1,1.2,1.21
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: April 7, 1999