Creating MHTML Formatted Messages

Creating MHTML Formatted Messages

MHTML formatting provides a simple way to send portions of a Web page or an entire Web page as a message. The message can include all of the HTML formatting, graphics, and other elements 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 Uniform Resource Locator (URL) parameter and a flags parameter. The URL specifies the Web page, and the optional flags specify the types of elements to exclude when downloading the page. When successfully completed, the page comprises the HTML formatted body of the message, and each graphic or other element referenced but not excluded in included as a MIME body part. By default, the text version (IMessage.TextBody) of the message is also automatically generated fro the HTML version (IMessage.HTMLBody) of the message.

The specified source URL must be accessible to create the message. If a proxy server is required to reach a URL outside a firewall, set this location for the https://schemas.microsoft.com/cdo/configuration/urlproxyserver field in the Configuration objectassociated with the Message object.

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

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.

' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Windows 2000 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") = "proxyserver: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

' 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.
iMsg.CreateMHTMLBody "http://server.example.com", _
                     cdoSuppressAll, _
                     "domain\username", _
                     "password"

' ...

#import "c:\program files\common files\system\ado\msado15.dll" no_namespace
#import <cdosys.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
{
  /*
  ** 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.
  */
  iMsg->CreateMHTMLBody(
           "http://server.example.com",
           cdoSuppressAll,
           "domain\\username",
           "password");
}
catch(_com_error err)
{
  // handle exception
}
Const g_Debug = True
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

On Error Resume Next

iMsg.CreateMHTMLBody "http://server.example.com", cdoSuppressAll

If Err.Number <> 0 Then
  ' Handle error.
End If

If g_Debug Then
  MsgBox iMsg.GetStream.ReadText
End if

   Note To suppress multiple elements, add the values of the flag constants; for example, to suppress images and style sheets, specify cdoSuppressImages + cdoSuppressStyleSheets in the flags parameter.

See Also

Concepts

urlproxyserver Field
CreateMHTMLBody Method
CdoMHTMLFlags Enum