Sample code for 30+ languages & platforms
PureBasic

Set a MIME Body, Preserving Content Headers

See more MIME Examples

Demonstrates the Chilkat Mime.SetBody method, which sets the current part's body while preserving the existing content headers (Content-Type, charset, Content-Transfer-Encoding). The only argument is the body text; it is a void method.

Background: Unlike the SetBodyFrom... methods, which set both the content and the headers that describe it, SetBody replaces only the body text and leaves the existing Content-Type and encoding in place. That is what you want when the media type is already correct and you simply need to swap in new content — updating a templated part, for instance — without re-declaring its type.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkMime.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Mime.SetBody method, which sets the current part's body while preserving the
    ;  existing content headers (Content-Type, charset, Content-Transfer-Encoding).  The only argument
    ;  is the body text.  It is a void method.

    mime.i = CkMime::ckCreate()
    If mime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Establish the content headers first.
    success = CkMime::ckSetBodyFromPlainText(mime,"initial")
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    ;  Replace just the body text, keeping the existing Content-Type and encoding.
    CkMime::ckSetBody(mime,"This is the replacement body text.")

    mimeText.s = CkMime::ckGetMime(mime)
    If CkMime::ckLastMethodSuccess(mime) = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    Debug mimeText


    CkMime::ckDispose(mime)


    ProcedureReturn
EndProcedure