Microsoft KB Archive/171098

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

Article ID: 171098

Article Last Modified on 5/9/2005

-

APPLIES TO


 * Microsoft Collaboration Data Objects 1.0
 * Microsoft Collaboration Data Objects 1.0a
 * Microsoft Collaboration Data Objects 1.1
 * Microsoft Collaboration Data Objects 1.2
 * Microsoft Collaboration Data Objects 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  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  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  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  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  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  no_namespace // cdo.dll is in \winnt\system32 on WinNT or  // \Win95\System directory on Win95 w/Exchange is installed.

#include   #include    #include 

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

