Platform SDK: CDO for Windows 2000

Setting Message Header Fields

The most common header fields for messages are exposed as properties on the IMessage interface; however, all header fields are accessible in the IMessage.Fields collection. The header fields you can set using this collection reside in the urn:schemas:mailheader: and urn:schemas:httpmail: namespaces. Additionally, the http://schemas.microsoft.com/exchange/sensitivity field is available.

You can add mail headers not present in the default list provided by adding the header to the collection within the urn:schemas:mailheader: field namespace. Simply add the header name after the "urn:schemas:mailheader:" string. For example, "urn:schemas:mailheader:myheader".

Note   Make sure to encode non US-ASCII characters using the mechanism defined in Request For Comments (RFC) 1522 when you set fields within the urn:schemas:mailheader: namespace. Non US-ASCII characters present in the string will be corrupted.

[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

Dim Flds as ADODB.Fields
Set Flds = iMsg.Fields

With Flds
  .Item("urn:schemas:httpmail:to")      = "someone@microsoft.com"
  .Item("urn:schemas:httpmail:from")   = "another@microsoft.com"
  .Item("urn:schemas:httpmail:cc")     = "thirdperson@microsoft.com"
  .Item("urn:schemas:httpmail:sender") = "myaddress@microsoft.com"

  .Item("urn:schemas:mailheader:myhdr") = "some value"
  .Item("urn:schemas:mailheader:x-hdr") = "less value"

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

FieldsPtr Flds;
Flds = iMsg->Fields;

Flds->Item["urn:schemas:httpmail:to"]->Value
      = _variant_t("someone@microsoft.com");
Flds->Item["urn:schemas:httpmail:from"]->Value
      = _variant_t("another@microsoft.com");
Flds->Item["urn:schemas:httpmail:cc"]->Value
      = _variant_t("thirdperson@microsoft.com");
Flds->Item["urn:schemas:httpmail:sender"]->Value
      = _variant_t("myaddress@microsoft.com");

Flds->Item["urn:schemas:mailheader:myhdr"]->Value
     = _variant_t("some value");
Flds->Item["urn:schemas:mailheader:x-header"]->Value
     = _variant_t("another value");
Flds->Update();
[VBScript]
Dim iMsg
Set iMsg = CreateObject("CDO.Message")

Dim Flds
Set Flds = iMsg.Fields

With Flds
  .Item("urn:schemas:httpmail:to")      = "someone@microsoft.com"
  .Item("urn:schemas:httpmail:from")   = "another@microsoft.com"
  .Item("urn:schemas:httpmail:cc")     = "thirdperson@microsoft.com"
  .Item("urn:schemas:httpmail:sender") = "myaddress@microsoft.com"
  .Item("urn:schemas:mailheader:myhdr") = "some value"
  .Item("urn:schemas:mailheader:x-Hdr") = "less value"
  .Update
End With

Tip   When using the Fields collection, always remember to call the Update method to commit any changes, deletions, or additions.

Note that in the example above, the Field.Item method was used to retrieve the Field object from the collection. You can also use the Fields.Append method. For Microsoft Collaboration Data Objects (CDO) applications, the Item method will return the appropriate Field object regardless of whether it currently exists in the collection.

See Also

IMessage Interface

urn:schemas:mailheader: Namespace

urn:schemas:httpmail: Namespace

http://schemas.microsoft.com/exchange/sensitivity Field