Specifying the Header Fields Character Set

Specifying the Header Fields Character Set

A message formatted in MIME can use one character set for the text or HTML body parts and another character set for the message header fields. When that is the case, the character set for the header fields must be explicitly set so that the fields are properly encoded using the mechanism defined by Request For Comments (RFC) 1522.

The following example demonstrates how to explicitly set the character set for the message header fields to iso-2022-jp, whereas the explicit character set for an included HTML body part is iso-8859-9.

' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Windows 2000 Library
Dim iMsg As New CDO.Message
With iMsg
  .From = """[Unicode characters]"" <example@example.com>"
  .To = """[Unicode characters]"" <example@example.com>"
  .Subject = "[Unicode characters]"

  ' The character set is iso-8859-1 for the HTML in included web page
  ' and for the text/plain alternative message text
  .CreateMHTMLBody "http://server.example.com"

  ' The next line sets the character set for the message header fields
  ' to iso-2022-jp.
  .BodyPart.Charset = "iso-2022-jp"
  .Send
End With

#import "c:\program files\common files\system\ado\msado15.dll" no_namespace
#import <cdosys.dll> no_namespace
// ...
IMessagePtr iMsg(__uuidof(Message));
iMsg->To   = """[Unicode characters]"" <example@example.com>";
iMsg->From = """[Unicode characters]"" <example@example.com>";
iMsg->Subject = "[Unicode characters]";
  /*
  **  The character set is iso-8859-1 for the HTML in the
  **  included web page and for the text/plain alternative message text
  */
iMsg->CreateMHTMLBody("http://server.example.com",cdoSuppressNone,"","");

  /*
  ** Now, set the character set for the message header fields
  ** so that they are encoded properly (RFC 1522).
  */
IBodyPartPtr iBp;
iBp = iMsg;
iBp->Charset = "iso-2022-jp";

iMsg->Send();
Const g_Debug = True
Dim iMsg
Set iMsg = CreateObject("CDO.Message")
With iMsg
  .From    = """[Unicode characters]"" <example@example.com>"
  .To      = """[Unicode characters]"" <example@example.com>"
  .Subject = "[Unicode characters]"

  ' The character set is iso-8859-1 for the HTML in included web page
  ' and for the text/plain alternative message text
  .CreateMHTMLBody "http://server.example.com"

  ' This next line sets the character set for the message header fields
  ' to iso-2022-jp.
  .BodyPart.Charset = "iso-2022-jp"

  If g_Debug Then
   ' Print Message Stream
   MsgBox .GetStream.ReadText
  End If
End With

See Also

Concepts

Charset Property
content-type Field