Sample code for 30+ languages & platforms
Xbase++

Add a MIME Header Field

See more MIME Examples

Demonstrates the Chilkat Mime.AddHeaderField method, which appends a new header field. The first argument is the field name and the second is the value. Existing fields with the same case-insensitive name are preserved, so this can create duplicates.

Background: MIME headers are ordered name/value lines, and some — Received, for instance — legitimately appear more than once. AddHeaderField is the tool for that: it always appends, never replacing an existing field of the same name. When you instead want exactly one field with a given name, use SetHeaderField, which replaces rather than accumulates.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oMime
LOCAL cHeaders

nSuccess := 0

//  Demonstrates the Mime.AddHeaderField method, which appends a new header field.  The 1st argument
//  is the field name and the 2nd is the value.  Existing fields with the same (case-insensitive)
//  name are preserved, so this can create duplicate fields.

oMime := CreateObject("Chilkat.Mime")

nSuccess := oMime:SetBodyFromPlainText("Hello.")
IF (nSuccess == 0)
    ? oMime:LastErrorText
    oMime:destroy()
    RETURN
ENDIF

//  Append header fields.  AddHeaderField does not replace existing fields of the same name.
nSuccess := oMime:AddHeaderField("X-Custom-Header", "value1")
IF (nSuccess == 0)
    ? oMime:LastErrorText
    oMime:destroy()
    RETURN
ENDIF

nSuccess := oMime:AddHeaderField("X-Priority", "3")
IF (nSuccess == 0)
    ? oMime:LastErrorText
    oMime:destroy()
    RETURN
ENDIF

cHeaders := oMime:GetEntireHead()
IF (oMime:LastMethodSuccess == 0)
    ? oMime:LastErrorText
    oMime:destroy()
    RETURN
ENDIF

? cHeaders

oMime:destroy()