Sample code for 30+ languages & platforms
DataFlex

Prepend Added Header Fields to the Top

See more Email Object Examples

Demonstrates the Chilkat Email.PrependHeaders property. When true, header fields added through AddHeaderField or AddHeaderField2 are placed at the top of the header rather than appended at the bottom (the default is false). This affects only fields added after the property is set — it does not reorder fields that are already present. This example enables prepending and adds two custom header fields.

Background: In a MIME message the order of header fields is mostly cosmetic, but there are cases where "top vs bottom" matters — for instance, trace headers like Received are conventionally added to the top so the most recent hop appears first, and some pipelines expect certain fields early in the header block. PrependHeaders gives you control over placement when you add your own custom fields.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoEmail
    String sTemp1

    //  Demonstrates the Email.PrependHeaders property.  When true, header fields added via
    //  AddHeaderField (or AddHeaderField2) are prepended to the top of the header instead of
    //  appended to the bottom.  The default is false.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Prepend headers demo"
    Set ComFrom Of hoEmail To "mary@example.com"

    //  Cause subsequently-added header fields to go to the top of the header.
    Set ComPrependHeaders Of hoEmail To True

    Send ComAddHeaderField To hoEmail "X-Custom-One" "first value"
    Send ComAddHeaderField To hoEmail "X-Custom-Two" "second value"

    Get ComHeader Of hoEmail To sTemp1
    Showln sTemp1


End_Procedure