Platform SDK: CDO for Windows 2000

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.

[Visual Basic]
' 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    = """[Japanese Unicode characters]"" <sender@microsoft.com>"
  .To      = """[Japanese Unicode characters]"" <recipient@microsoft.com>"
  .Subject = "[Japanese 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://www.microsoft.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
[C++,IDL]
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace
#import <cdosys.dll> no_namespace
// ...
IMessagePtr iMsg(__uuidof(Message));
iMsg->To   = """[Japanese Unicode characters]"" <sender@microsoft.com>";
iMsg->From = """[Japanese Unicode characters]"" <recipient@microsoft.com>";
iMsg->Subject = "[Japanese 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://www.microsoft.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();
[VBScript]
Const g_Debug = True
Dim iMsg
Set iMsg = CreateObject("CDO.Message")
With iMsg 
  .From    = """[Japanese Unicode characters]"" <sender@microsoft.com>"
  .To      = """[Japanese Unicode characters]"" <recipient@microsoft.com>"
  .Subject = "[Japanese 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://www.microsoft.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

IBodyPart.Charset Property

urn:schemas:mailheader:content-type Field