Setting Subject and TextBody Fields

Topic Last Modified: 2006-06-12

When sending a message, setting the subject field is optional, but usually specified. For posting a message to a newsgroup, setting the subject field is required. For both message types, setting the text body field is optional, but also typically provided.

You can set the subject and text body fields using the IMessage.Subject and IMessage.TextBody properties.

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

With iMsg
  .To = "someone@example.com"
  .Newsgroups = "comp.example.newsgroup1"
  .Subject = "Agenda for staff meeting"
  .TextBody = "Please plan to present your status for the following projects..."
End With

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));

iMsg->To         = "someone@example.com";
iMsg->Newsgroups = "comp.example.newsgroup1";
iMsg->Subject    = "Agenda for staff meeting";
iMsg->TextBody   = "Please plan to present your status for the following projects...";

VBScript

Dim iMsg 
Set iMsg = CreateObject("CDO.Message")

With iMsg
  .To         = "someone@example.com"
  .Newsgroups = "comp.example.newsgroup1"
  .Subject    = "Agenda for staff meeting"
  .TextBody   = "Please plan to present your status for the following projects..."
End With

You can use the IMessage.Fields collection to set the subject and text body fields of the message. The field names are urn:schemas:mailheader:subject, urn:schemas:httpmail:subject, and urn:schemas:httpmail:textdescription, respectively. The subject field within the urn:schemas:mailheader: returns the subject with any non-US-ASCII characters encoded according to RFC 1522. The subject field within the urn:schemas:httpmail: namespace contains the subject in Unicode characters.

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 Flds As ADODB.Fields
Set Flds = iMsg.Fields
With Flds
  .Item("urn:schemas:httpmail:to") = "someone@example.com"
  .Item("urn:schemas:httpmail:from") = "<another@example.com>"
  .Item("urn:schemas:httpmail:cc") = "<thirdperson@example.com>"

  .Item("urn:schemas:httpmail:subject") = "Agenda for the staff"
meeting ""
  .Item("urn:schemas:httpmail:textdescription") = "Please plan to present your status for the following projects..."
  .Update
End With

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));

FieldsPtr Flds;
Flds = iMsg->Fields;
Flds->Item["urn:schemas:mailheader:to"] =
_variant_t("someone@example.com");
Flds->Item["urn:schemas:mailheader:from"] =
_variant_t("<another@example.com>");
Flds->Item["urn:schemas:mailheader:cc"] =
_variant_t("<thirdperson@example.com>");
Flds->Item["urn:schemas:mailheader:subject"]->Value         
     = _variant_t("Agenda for the staff meeting");
Flds->Item["urn:schemas:mailheader:textdescription"]->Value
     = _variant_t("Please plan to present your status for the following
projects...");
Flds->Update();

VBScript

Dim iMsg
Set iMsg = CreateObject("CDO.Message")

Dim Flds
Set Flds = iMsg.Fields
With Flds
  .Item("urn:schemas:httpmail:to")   = "someone@example.com"
  .Item("urn:schemas:httpmail:from") = "<another@example.com>"
  .Item("urn:schemas:httpmail:cc")   = "<thirdperson@example.com>"

  .Item("urn:schemas:httpmail:subject")         = "Agenda for the staff
meeting"
  .Item("urn:schemas:httpmail:textdescription") = "Please plan to present
your status for the following projects..."
  .Update
End With

In addition to a plain text body, you can use HTML-formatted text using the IMessage.HTMLBody property. By default, the IMessage.AutoGenerateTextBody property in the object is set to True (VARIANT_TRUE), and therefore a plain text body is generated automatically from the HTML body text to support recipients whose clients cannot display HTML-formatted messages. For more information about creating attachments, see Creating MIME-Formatted Messages.