How To Send a Message Using CDO (1.x) with VC++ (171098)



The information in this article applies to:

  • Collaboration Data Objects (CDO) 1.0
  • Collaboration Data Objects (CDO) 1.0a
  • Collaboration Data Objects (CDO) 1.1
  • Collaboration Data Objects (CDO) 1.2
  • Collaboration Data Objects (CDO) 1.21

This article was previously published under Q171098

SUMMARY

This article gives sample code written in Visual C++ that may be used to send a message with CDO (1.x). 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.

   // TO DO: If you are using CDO (1.0, 1.0a) unremark the next 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 CDO (1.1) unremark the next 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, 1.21) unremark the next 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 (1.0, 1.0a, 1.1) uncomment the next line
       // SessionPtr pSession("MAPI.Session");
       // For CDO (1.2, 1.21) 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";
       // Type can be ActMsgTo, mapiTo or CdoTo for different CDO versions
       // They all have constant value of 1.
       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 the CDO (1.x) Libraries, see the following article in the Microsoft Knowledge Base:

171440 INFO: Where to Acquire the CDO (all versions) Libraries


Modification Type:MajorLast Reviewed:5/9/2005
Keywords:kbcode kbhowto kbMsg KB171098