Sample code for 30+ languages & platforms
AutoIt

Set a MIME Header Field

See more MIME Examples

Demonstrates the Chilkat Mime.SetHeaderField method, which sets a single header field. The first argument is the field name and the second is the value. All existing occurrences are removed and one replacement is inserted, or a new field is added if none exists.

Background: For headers that should appear exactly once — Subject, From, Content-Type — this is the method to use, because it guarantees a single occurrence no matter how many times you call it. That idempotence is the key difference from AddHeaderField, which would leave duplicates behind. Setting an empty value removes the field.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Demonstrates the Mime.SetHeaderField method, which sets a single header field.  The 1st argument
;  is the field name and the 2nd is the value.  All existing occurrences are removed and one
;  replacement is inserted (or a new field is added if none exists).

$oMime = ObjCreate("Chilkat.Mime")

$bSuccess = $oMime.SetBodyFromPlainText("Hello.")
If ($bSuccess = False) Then
    ConsoleWrite($oMime.LastErrorText & @CRLF)
    Exit
EndIf

;  Unlike AddHeaderField, SetHeaderField ensures exactly one field with this name.
$bSuccess = $oMime.SetHeaderField("Subject","Weekly Report")
If ($bSuccess = False) Then
    ConsoleWrite($oMime.LastErrorText & @CRLF)
    Exit
EndIf

;  Setting it again replaces the previous value rather than adding a duplicate.
$bSuccess = $oMime.SetHeaderField("Subject","Weekly Report (revised)")
If ($bSuccess = False) Then
    ConsoleWrite($oMime.LastErrorText & @CRLF)
    Exit
EndIf

Local $subject = $oMime.GetHeaderField("Subject")
If ($oMime.LastMethodSuccess = False) Then
    ConsoleWrite($oMime.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Subject: " & $subject & @CRLF)