Creating MHTML-Formatted Messages

Topic Last Modified: 2006-06-12

MIME Encapsulation of Aggregate HTML Documents (MHTML) formatting provides a simple way to send an entire Web page, or portions of one, as a message. The message includes all of the HTML formatting, graphics, and other elements that are contained in the original page. You can also choose to exclude one or more types of elements from the message.

The IMessage.CreateMHTMLBody method accepts a URL parameter and a flags parameter. The URL specifies the Web page being sent, and the optional flags specify the types of elements to exclude from the message. Collaboration Data Objects (CDO) makes the file specified by the URL the IMessage.HTMLBody of the message, and each graphic or other element referenced in that file becomes a Multipurpose Internet Mail Extensions (MIME) body part. By default, CDO automatically generates the text body that contains a plain text version of the HTMLBody content.

CDO must be able to access the specified source URL to create the message. If CDO needs to use a proxy server to reach a URL outside a firewall, set this location for the https://schemas.microsoft.com/cdo/configuration/urlproxyserver field in the Message object's associated Configuration object.

The following example shows how to create a message from a Web page.

Example

Visual Basic

' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Exchange 2000 Server Library
' ..
Dim iMsg  As New CDO.Message
Dim iConf As New CDO.Configuration
Dim Flds  As ADODB.Fields

Set Flds = iConf.Fields
Flds("https://schemas.microsoft.com/cdo/configuration/urlproxyserver") = "proxyname:80"
Flds("https://schemas.microsoft.com/cdo/configuration/urlproxybypass") = "<local>"
Flds("https://schemas.microsoft.com/cdo/configuration/urlgetlatestversion") = True
Flds.Update

Set iMsg.Configuration = iConf

' Note: It is recommended that the password not be stored in the source code.
iMsg.CreateMHTMLBody "http://www.example.com", _
                     cdoSuppressAll, _
                     "domain\username", _
                     "password"

' ...

C++, IDL

#import "c:\program files\common files\system\ado\msado15.dll" no_namespace
#import "c:\program files\common files\microsoft shared\cdo\cdoex.dll" no_namespace
// ...
IMessagePtr       iMsg(__uuidof(Message));
IConfigurationPtr iConf(__uuidof(Configuration));
FieldsPtr         Flds;

Flds = iConf->Fields;
Flds
>Item["https://schemas.microsoft.com/cdo/configuration/urlproxyserver"]
>Value
  = _variant_t("myproxyserver:80");
Flds
>Item["https://schemas.microsoft.com/cdo/configuration/urlproxybypass"]
>Value
  = _variant_t("<local>");
Flds
>Item["https://schemas.microsoft.com/cdo/configuration/urlgetlatestversion
]->Value
  = _variant_t(VARIANT_TRUE);
Flds->Update();
iMsg->Configuration = iConf;
try
{
  // Note: It is recommended that the password not be stored in the source code.
  iMsg->CreateMHTMLBody(
           "http://www.example.com",
           cdoSuppressAll,
           "domain\\username",
           "password");
}
catch(_com_error err)
{
  // handle exception
}

VBScript

Dim iMsg
Dim iConf
Dim Flds

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

Set Flds = iConf.Fields
Flds("https://schemas.microsoft.com/cdo/configuration/urlproxyserver") =
"proxyname:80"
Flds("https://schemas.microsoft.com/cdo/configuration/urlproxybypass") =
"<local>"
Flds("https://schemas.microsoft.com/cdo/configuration/urlgetlatestversion")
= True
Flds.Update

Set iMsg.Configuration = iConf

' Note: It is recommended that the password not be stored in the source code.
iMsg.CreateMHTMLBody "http://www.example.com", _
                      cdoSuppressAll, _
                      "domain\username", _
                      "password"

Note

To suppress multiple elements, add the values of the flag constants. For example, to suppress images and style sheets, specify the flags parameter as (cdoSuppressImages + cdoSuppressStyleSheets).