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

Last reviewed: March 2, 1998
Article ID: Q171098
The information in this article applies to:
  • Collaboration Data Objects (CDO), versions 1.0, 1.0a, 1.1

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.

   #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
     {
       // Create a MAPI.Session pointer
       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 Active Messaging Libraries

Keywords          : kbcode ActMsg OLEMSG
Technology        : kbole
Version           : WINDOWS:1.0,1.0a,1.1
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 2, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.