HOWTO: Sending a Message via Active Messaging with VC++

ID: Q171098

The information in this article applies to:

SUMMARY

This article gives sample code written in Visual C++ that may be used to send a message with Active Messaging. This sample is based on the code provided with the Microsoft Visual C++ 5.0 COMMAIL sample project.

MORE INFORMATION

Before compiling and running the following code, be sure to follow the instructions given wherever the text "TO DO:" is found.

Sample Code

   /**********************************************************/ 
   // COMMAIL.CPP
   // This program sends a message with an attachment
   /**********************************************************/ 

   // TO DO: If you are using Active Messaging 1.0 or 1.0a,
   // unremark the following line:
   // #import <mdisp32.tlb> no_namespace
   // mdisp32.tlb is in \winnt\system32 on Windows NT or
   // \Win95\System directory on Windows 95 when Exchange is installed.

   // TO DO: If you are using Active Messaging 1.1,
   // unremark the following line:
   // #import <olemsg32.dll> no_namespace
   // olemsg32.dll is in \winnt\system32 on Windows NT or
   // \Win95\System directory on Windows 95 when Exchange is installed.

   // TO DO: If you are using CDO 1.2 or 1.2.1, unremark the
   // following line:
   // #import <cdo.dll> no_namespace
   // cdo.dll is in \winnt\system32 on WinNT or
   // \Win95\System directory on Win95 w/Exchange is installed.

   #include <stdio.h>
   #include <assert.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
     {

       // TO DO: Create a MAPI.Session pointer
       // For CDO versions prior to 1.2 uncomment the next line
       //SessionPtr pSession("MAPI.Session");
       // For CDO versions 1.2 or higher uncomment the next line
       //_SessionPtr pSession("MAPI.Session");

       // Logon using the specified profile
       pSession->Logon("TO DO: Place profile name here");

       // Create pointer to the Outbox Folder
       FolderPtr   pFolder = pSession->Outbox;

       // Create pointer to the Messages Collection
       MessagesPtr pMessages = pFolder->Messages;

       // Create pointer to a new Message
       MessagePtr  pMessage = pMessages->Add();

       // Set the Subject of the message
       pMessage->Subject = "VCCOM: MAPI Example";

       // Set the Text of the message
       pMessage->Text = "This is my message text.";

       // Create pointer to Attachments collection
       AttachmentsPtr pAttachments = pMessage->Attachments;

       // Create new Attachment
       // TO DO: Change the absolute path (which must be used)
       pAttachments->Add("Mapi example source code.txt", 15000L,
                         (long) mapiFileData,
                         "c:\\mapitest\\commail\\commail.cpp");

       // Create pointer to Recipients Collection
       RecipientsPtr pRecipients = pMessage->Recipients;

       // Create pointer to a new Recipient
       RecipientPtr pRecipient = pRecipients->Add();

       // Set properties of the new recipient
       pRecipient->Name = "TO DO: Place email alias here";
       pRecipient->Type = (long) mapiTo;

       // Resolve the recipient address
       pRecipient->Resolve();

       _tprintf(_T("Sending message to %s.\n"),
                   (LPCTSTR) (bstr_t) pRecipient->Name);

       // Send the message
       pMessage->Send(false, false);

       // Logoff of the MAPI Session
       pSession->Logoff();

     }
     catch (_com_error &e)
     {
         dump_com_error(e);
     }
   }

REFERENCES

For additional information about Collaboration Data Objects versus Active Messaging, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176916
   TITLE     : INFO: Active Messaging and Collaboration Data Objects (CDO)

For more information on Active Messaging Libraries please see the following article in the Microsoft Knowledge Base:

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

Keywords          : kbcode kbActMsg kbCDO100 kbCDO100a kbCDO110 kbCDO120 kbCDO121 kbVC OleMsg 
Version           : WINDOWS:1.0,1.0a,1.1,1.21
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: April 9, 1999