Sample code for 30+ languages & platforms
Xbase++

Enumerate MIME Header Field Names

See more MIME Examples

Demonstrates the Chilkat Mime.GetHeaderFieldName method, which returns the name of the header-field occurrence at a zero-based index. The only argument is the index; use NumHeaderFields for the count. Original order and casing are preserved.

Note: The file path is relative to the application's current working directory. Absolute paths may also be used. Supply the path to your own file.

Background: When you need to walk all the headers — to display them, copy them, or handle duplicates that GetHeaderField would hide — index-based access is the way. NumHeaderFields bounds the loop, and this returns each field's name in its original order and casing, which matters when the exact header sequence is significant.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oMime
LOCAL n
LOCAL i
LOCAL cFieldName

nSuccess := 0

oMime := CreateObject("Chilkat.Mime")
nSuccess := oMime:LoadMimeFile("qa_data/message.eml")
IF (nSuccess == 0)
    ? oMime:LastErrorText
    oMime:destroy()
    RETURN
ENDIF

//  Iterate the header fields by index.  NumHeaderFields gives the count; GetHeaderFieldName and
//  GetHeaderFieldValue return the name and value at each zero-based index.  Original order and
//  casing are preserved.
n := oMime:NumHeaderFields
? "Header fields: " + Str(n)

FOR i := 0 TO n - 1
    cFieldName := oMime:GetHeaderFieldName(i)
    IF (oMime:LastMethodSuccess == 0)
        ? oMime:LastErrorText
        oMime:destroy()
        RETURN
    ENDIF

    ? cFieldName
NEXT

oMime:destroy()