Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Mime
string ls_Subject

li_Success = 0

//  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).

loo_Mime = create oleobject
li_rc = loo_Mime.ConnectToNewObject("Chilkat.Mime")
if li_rc < 0 then
    destroy loo_Mime
    MessageBox("Error","Connecting to COM object failed")
    return
end if

li_Success = loo_Mime.SetBodyFromPlainText("Hello.")
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

//  Unlike AddHeaderField, SetHeaderField ensures exactly one field with this name.
li_Success = loo_Mime.SetHeaderField("Subject","Weekly Report")
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

//  Setting it again replaces the previous value rather than adding a duplicate.
li_Success = loo_Mime.SetHeaderField("Subject","Weekly Report (revised)")
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

ls_Subject = loo_Mime.GetHeaderField("Subject")
if loo_Mime.LastMethodSuccess = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

Write-Debug "Subject: " + ls_Subject


destroy loo_Mime