Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMime
    String sMimeText
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End

    //  Establish the content headers first.
    Get ComSetBodyFromPlainText Of hoMime "initial" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Replace just the body text, keeping the existing Content-Type and encoding.
    Send ComSetBody To hoMime "This is the replacement body text."

    Get ComGetMime Of hoMime To sMimeText
    Get ComLastMethodSuccess Of hoMime To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sMimeText


End_Procedure