Setting Message Header Fields
Topic Last Modified: 2006-06-12
The most common message header fields 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 by 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 other headers not present in the provided default schema by adding the header to the collection within the urn:schemas:mailheader: field namespace.
Note
Make sure to encode non-US-ASCII characters when using the urn:schemas:mailheader: namespace using the mechanism defined in Request for Comments (RFC) 1522.
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
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:sender") = "myaddress@example.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 "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:httpmail:to"]->Value
= _variant_t("someone@example.com");
Flds->Item["urn:schemas:httpmail:from"]->Value
= _variant_t("another@example.com");
Flds->Item["urn:schemas:httpmail:cc"]->Value
= _variant_t("thirdperson@example.com");
Flds->Item["urn:schemas:httpmail:sender"]->Value
= _variant_t("myaddress@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();
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:sender") = "myaddress@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, remember to invoke the Update method to commit any changes, deletions, or additions. If the Collaboration Data Objects (CDO) object is currently bound, invoke IDataSource.Save to commit the changes to the data source.
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 CDO applications, the Item method will return the appropriate Field object regardless of whether it currently exists in the collection.