Setting Message Header Fields

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 https://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.

' 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") = "example@example.com"
  .Item("urn:schemas:httpmail:from") = "another@example.com"
  .Item("urn:schemas:httpmail:cc") = "exampleuser3@example.com"
  .Item("urn:schemas:httpmail:sender") = "example@example.com"

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

  .Update
End With

#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("example@example.com");
Flds->Item["urn:schemas:httpmail:from"]->Value
      = _variant_t("another@example.com");
Flds->Item["urn:schemas:httpmail:cc"]->Value
      = _variant_t("exampleuser3@example.com");
Flds->Item["urn:schemas:httpmail:sender"]->Value
      = _variant_t("example@example.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();
Dim iMsg
Set iMsg = CreateObject("CDO.Message")

Dim Flds
Set Flds = iMsg.Fields

With Flds
  .Item("urn:schemas:httpmail:to")      = "example@example.com"
  .Item("urn:schemas:httpmail:from")   = "another@example.com"
  .Item("urn:schemas:httpmail:cc")     = "exampleuser3@example.com"
  .Item("urn:schemas:httpmail:sender") = "example@example.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

Concepts

IMessage Interface
urn:schemas:mailheader: Namespace
urn:schemas:httpmail: Namespace
sensitivity Field