Creating and Sending a Message
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.
Important Storing user names and passwords inside source code can lead to security vulnerabilities in your software. Do not store user names and passwords in your production code.
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(cdoSMTPServer) = "server.example.com"
.Item(cdoSMTPConnectionTimeout) = 10 ' quick timeout
.Item(cdoSMTPAuthenticate) = cdoBasic
' IMPORTANT: Storing user names and passwords inside source code
' can lead to security vulnerabilities in your software. Do not
' store user names and passwords in your production code.
.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"" <example@example.com>"
.From = """User B"" <exampleB@example.com>"
.Subject = "Hows it going? I've attached my web page"
.CreateMHTMLBody "http://example.com"
.AddAttachment "C:\files\mybook.doc"
.Send
End With
#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[cdoSMTPServer]->Value =
_variant_t("example@example.com");
Flds->Item[cdoSMTPConnectionTimeout]->Value = _variant_t((long)10);
Flds->Item[cdoSMTPAuthenticate]->Value = _variant_t(cdoBasic);
/*
** IMPORTANT: Storing user names and passwords inside source code
** can lead to security vulnerabilities in your software. Do not
** store user names and passwords in your production code.
*/
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\" <example@server.example.com>";
iMsg->From = "\"Another\" <another@example.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://example.com",
cdoSuppressAll,
"",
"");
}
catch (_com_error e)
{
//...
}
iMsg->Send();
// save message
Stm = iMsg->GetStream();
Stm->SaveToFile(_bstr_t("message.eml"),adSaveCreateOverWrite);
}
CoUninitialize();
}
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(cdoSMTPServer) = "server.example.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"" <example@example.com>"
.From = """User B"" <exampleB@example.com>"
.Subject = "Hows it going? I've attached my web page"
.CreateMHTMLBody "http://example.com"
.AddAttachment "C:\files\mybook.doc"
.Send
End With