Sample code for 30+ languages & platforms
DataFlex

Add Duplicate Header Fields to an Email

See more Email Object Examples

Demonstrates the Chilkat Email.AddHeaderField2 method. It works like AddHeaderField, except that if the header field already exists it is not replaced — a duplicate header is added instead. Use it for headers that may legally occur more than once, such as Received. This example adds two headers with the same name and prints the header showing both.

Background: The email standards allow certain header fields to repeat. The classic case is Received: each mail server that handles a message prepends its own Received line, so a delivered email typically has several, forming a trace of the path it took. Single-valued fields like Subject should be replaced (use AddHeaderField), whereas repeatable fields need AddHeaderField2 so earlier occurrences are preserved rather than overwritten.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoEmail
    String sTemp1

    //  Demonstrates the AddHeaderField2 method.  It is the same as AddHeaderField, except that
    //  if the header field already exists, it is NOT replaced -- a duplicate header is added.
    //  Use this for headers that may legally occur more than once, such as "Received".

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Duplicate header example"
    Set ComFrom Of hoEmail To "alice@example.com"

    //  Add two header fields with the same name; both are retained.
    Send ComAddHeaderField2 To hoEmail "X-Trace" "hop-1"
    Send ComAddHeaderField2 To hoEmail "X-Trace" "hop-2"

    //  Both X-Trace fields are present in the header.
    Get ComHeader Of hoEmail To sTemp1
    Showln sTemp1


End_Procedure