Sample code for 30+ languages & platforms
DataFlex

Get a Header Field by Name

See more Email Object Examples

Demonstrates the Chilkat Email.GetHeaderField method, which returns the value of a header field by name. Header-field names are case-insensitive, so X-Priority and x-priority refer to the same field. This example reads several headers by name.

Background: Looking up a header by name is the quickest way to read a known field like Subject or a custom X- header. One caveat: some header names (such as Received) can legitimately appear more than once. When a field may repeat and you need every occurrence, enumerate by index with GetHeaderFieldName and GetHeaderFieldValue instead of looking up by name.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoEmail
    String sTemp1

    //  Demonstrates the GetHeaderField method, which returns the value of a header field by
    //  name.  Header-field names are case-insensitive.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Quarterly report"
    Set ComFrom Of hoEmail To "alice@example.com"
    Send ComAddHeaderField To hoEmail "X-Priority" "1"

    //  Get header field values by name.
    Get ComGetHeaderField Of hoEmail "Subject" To sTemp1
    Showln "Subject = " sTemp1
    Get ComGetHeaderField Of hoEmail "From" To sTemp1
    Showln "From = " sTemp1
    Get ComGetHeaderField Of hoEmail "x-priority" To sTemp1
    Showln "X-Priority = " sTemp1


End_Procedure