Platform SDK: CDO for Windows 2000

Creating and Sending a Message

The following example shows how to create a complete message that contains Hypertext Markup Language (HTML) and plain text versions and several attachments, and send it over the network.

[Visual Basic]
Dim iMsg as New CDO.Message
Dim iConf as New CDO.Configuration

Dim Flds as ADODB.Fields
Set Flds = iConf.Fields

With Flds
  .Item(cdoSendUsingMethod)       = cdoSendUsingPort
  .Item(cdoSMTPServerName)        = "somehost@microsoft.com"
  .Item(cdoSMTPConnectionTimeout) = 10 ' quick timeout
  .Item(cdoSMTPAuthenticate)      = cdoBasic
  .Item(cdoSendUserName)          = "username"
  .Item(cdoSendPassword)          = "password"
  .Item(cdoURLProxyServer)        = "server:80"
  .Item(cdoURLProxyBypass)        = "<local>"
  .Item(cdoURLGetLatestVersion)   = True
  .Update
End With

With iMsg
  Set .Configuration = iConf
      .To       = """User A"" <userA@microsoft.com>"
      .From     = """User B"" <userB@microsoft.com>"
      .Subject  = "Hows it going? I've attached my web page"
      .CreateMHTMLBody "http://mypage"
      .AddAttachment "C:\files\mybook.doc"
      .Send
End With
[C++,IDL]
#import "d:\program files\common files\system\ado\msado15.dll" no_namespace
#import <cdosys.dll> no_namespace
#include <iostream.h>

void main()
{
  CoInitialize(NULL);
  {
    IMessagePtr   iMsg(__uuidof(Message));
    IBodyPartPtr  iBp;
    IBodyPartsPtr iBps;
    _StreamPtr    Stm;
    _bstr_t       filepath;

    IConfigurationPtr iConf(__uuidof(Configuration));
    FieldsPtr         Flds;
    Flds         =    iConf->Fields;
    Flds->Item[cdoSendUsingMethod]->Value       = 
          _variant_t(cdoSendUsingPort);
    Flds->Item[cdoSMTPServerName]->Value        = 
          _variant_t("somehost@microsoft.com");
    Flds->Item[cdoSMTPConnectionTimeout]->Value = _variant_t((long)10);
    Flds->Item[cdoSMTPAuthenticate]->Value      = _variant_t(cdoBasic);
    Flds->Item[cdoSendUserName]->Value          = _variant_t("username");
    Flds->Item[cdoSendPassword]->Value          = _variant_t("password");
    Flds->Item[cdoURLProxyServer]->Value        = _variant_t("server:80");
    Flds->Item[cdoURLProxyBypass]->Value        = _variant_t("<local>");
    Flds->Item[cdoURLGetLatestVersion]->Value   = 
          _variant_t(VARIANT_TRUE);
    Flds->Update();

    iMsg->To        = "\"SomeOne\" <someone@fake.microsoft.com>";
    iMsg->From      = "\"Another\" <another@fake.microsoft.com>";
    iMsg->Subject   = "Here is a subject for the message.";

    /*
    ** attach an html file to message
    ** your local path goes in the "path"
    ** variable below
    */
    try 
    {
      filepath  = _bstr_t(path) + _bstr_t("\\htmlfile.html");
      iMsg->AddAttachment(filepath,"","");
    }
    catch (_com_error e)
    {
      //...
    }

    /*
    ** Attach Word 2000 file to message
    */
    try {
      filepath  = _bstr_t(path) + _bstr_t("\\wordfile.doc");
      iMsg->AddAttachment(filepath,"","");
    }
    catch (_com_error e)
    {
      //...
    }
    
    /*
    ** Create MTHML body.
    */
    try 
    {
      iMsg->CreateMHTMLBody(
                  "http://mypage", 
                  cdoSuppressAll,
                  "",
                  "");
    }
    catch (_com_error e)
    {
      //...
    }

    iMsg->Send();

    // save message
    Stm    = iMsg->GetStream();
    Stm->SaveToFile(_bstr_t("message.eml"),adSaveCreateOverWrite);

  }
  CoUninitialize();
}
[VBScript]
Dim iMsg
Set iMsg = CreateObject("CDO.Message")
Dim iConf 
Set iConf = CreateObject("CDO.Configuration")

Dim Flds
Set Flds = iConf.Fields

With Flds
  ' assume constants are defined within script file
  .Item(cdoSendUsingMethod)       = 2 ' cdoSendUsingPort
  .Item(cdoSMTPServerName)        = "somehost@microsoftc.om"
  .Item(cdoSMTPConnectionTimeout) = 10 ' quick timeout
  .Item(cdoSMTPAuthenticate)      = cdoBasic
  .Item(cdoSendUserName)          = "username"
  .Item(cdoSendPassword)          = "password"
  .Item(cdoURLProxyServer)        = "server:80"
  .Item(cdoURLProxyBypass)        = "<local>"
  .Item(cdoURLGetLatestVersion)   = True
  .Update
End With

With iMsg
  Set .Configuration = iConf
      .To       = """User A"" <userA@microsoft.com>"
      .From     = """User B"" <userB@microsoft.com>"
      .Subject  = "Hows it going? I've attached my web page"
      .CreateMHTMLBody "http://mypage"
      .AddAttachment "C:\files\mybook.doc"
      .Send
End With