Sample code for 30+ languages & platforms
PureBasic

Get the Name of the Nth Header Field

See more Email Object Examples

Demonstrates the Chilkat Email.GetHeaderFieldName method, which returns the name of the Nth header field. The NumHeaderFields property gives the count, and indexing is zero-based. This example enumerates the names of all header fields.

Background: Enumerating headers by index — rather than looking them up by name — lets you discover which fields a message actually contains and see repeated fields (like multiple Received lines) in order. Pair GetHeaderFieldName with GetHeaderFieldValue at the same index to walk the entire header block as name/value pairs.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    ;  Demonstrates the GetHeaderFieldName method, which returns the name of the Nth header
    ;  field.  The NumHeaderFields property gives the number of header fields; indexing is
    ;  zero-based.

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "Enumerate header names")
    CkEmail::setCkFrom(email, "alice@example.com")
    CkEmail::ckAddTo(email,"Bob","bob@example.com")

    n.i = CkEmail::ckNumHeaderFields(email)
    i.i
    For i = 0 To n - 1
        Debug "Header " + Str(i) + " name: " + CkEmail::ckGetHeaderFieldName(email,i)
    Next


    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure