Message Headers

Each message contains additional information about the message. The information is organized in header blocks at the beginning of the message and at the beginning of each section of a multipart message. Each header block contains one or more header fields, along with their values. For example, the sender of a message is specified in the "From" header field, with the value being the sender's e-mail address.

As discussed in the section on composing messages, there are a number of properties in the control which represent header fields in the current message. The From, To and Subject properties each correspond to their respective header field in the message. However, messages can have many more headers than those which are represented by properties in the control. To access these headers, there is the GetHeader method which can be used to obtain the value of a particular field. For example, one way to determine if a message was generated by Microsoft Outlook is to check for the presence of the X-MimeOLE header:

If InternetMail1.GetHeader("X-MimeOLE", strHeaderValue) Then
    MsgBox strHeaderValue, vbInformation
End If

It should be noted that unlike most of the other methods, the GetHeader method returns a boolean value, not an error code. The method returns True if the header field is present in the message and the second argument, a string variant, will contain the header value. If the header field does not exist, the method will return False.

There are a number of standard headers which are recognized by all mail clients and are typically those which are represented as properties in the control. However, implementers are free to create their own custom headers as long as they prefix them with "X-" (which indicates that they are a non-standard extension). While a mail client can check for the presence of extended headers, it should not depend on them being in the message. General purpose applications must be able to handle situations where a non-standard header is either missing or contains an unexpected value.

To change the value of a header, an application can use the SetHeader method. If the header field exists, its value will be replaced by the new value specified as an argument to the method. If the header field does not exist, it will be added to the current message. For example, to create a custom header field which contains a customer number, the following code could be used:

InternetMail1.SetHeader "X-Customer-Number", nCustomerNumber

Because the second argument to the SetHeader method is a variant, a variety of data types such as strings, integers, etc., may be passed as values. Note that data types which are affected by localization, such as date values, will be represented differently based on how the local system has been configured. Remember that the GetHeader and SetHeader methods use the current message part, so if your message is multipart (such as having a file attachment) then make sure that you first set the MessagePart property to a value of 0 to ensure you are in the main header block for the message.

In some circumstances you may need to list all of the header values in a message without knowing what header fields are present. You can do this by enumerating the header fields using the GetFirstHeader and GetNextHeader methods. The following example stores each header field and value in a pair of listboxes:

If InternetMail1.GetFirstHeader(strHeaderField, strHeaderValue) Then
    Do
        List1.AddItem strHeaderField
        List2.AddItem strHeaderValue
    Loop While InternetMail1.GetNextHeader(strHeaderField, strHeaderValue)
End If

To delete a message header, call the DeleteHeader method with the header field as the argument. If the header field exists, it will be deleted and the method will return a value of True. If the header does not exist, the method will return False. To delete all of the headers in a message, you could use code like this:

Do While InternetMail1.GetFirstHeader(strHeaderField, strHeaderValue)
    InternetMail1.DeleteHeader strHeaderField
Loop

Remember that a valid mail message requires a header block, so once the headers have been deleted, new values should be set for fields such as From, To, Date, Subject and so on. Also, keep in mind that changing or deleting header fields in a message should only be done when the effect of that change is well understood. In other words, simply deleting all of the headers to "clean up" a message is not recommended since it may damage the internal structure of the message, making it unreadable to other mail software.


Copyright © 2008 Catalyst Development Corporation. All rights reserved.