Sample code for 30+ languages & platforms
DataFlex

Remove a Header Field from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveHeaderField method, which removes by name all occurrences of a header field. Header-field names are case-insensitive, and every repeated occurrence with the given name is removed. This example adds a custom header, then removes it.

Background: Editing a message sometimes means deleting a header outright — stripping a tracking header, removing an X- flag before forwarding, or clearing a stale field before re-sending. Because a field name can appear more than once (like Received), RemoveHeaderField removes all matching occurrences in one call rather than just the first.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoEmail
    Boolean iHasBefore
    Boolean iHasAfter

    //  Demonstrates the RemoveHeaderField method, which removes by name all occurrences of a
    //  header field.  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 "Remove header field"
    Set ComFrom Of hoEmail To "alice@example.com"

    Send ComAddHeaderField To hoEmail "X-Custom-Header" "some value"

    //  Use HasHeaderMatching to test for the header's presence.  ("*" matches any value.)
    Get ComHasHeaderMatching Of hoEmail "X-Custom-Header" "*" False To iHasBefore
    Showln "Has X-Custom-Header before: " iHasBefore

    //  Remove all occurrences of the header field.
    Send ComRemoveHeaderField To hoEmail "X-Custom-Header"

    Get ComHasHeaderMatching Of hoEmail "X-Custom-Header" "*" False To iHasAfter
    Showln "Has X-Custom-Header after: " iHasAfter


End_Procedure