Sample code for 30+ languages & platforms
DataFlex

Get an Alternative Body into a BinData

See more Email Object Examples

Demonstrates the Chilkat Email.GetAlternativeBodyBd method, which copies the contents of the Nth alternative body into a BinData object. The first alternative body is at index 0, and it should only be called when NumAlternatives is greater than 0. This example builds a message with plain-text and HTML alternatives and copies the first into a BinData.

Background: This is the binary counterpart to GetAlternativeBody (which returns text). Reading an alternative into a BinData preserves the exact bytes — important when a body part uses a binary or unusual transfer encoding, or when you intend to hash it or write it out verbatim rather than decode it to a string.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    Variant vBd
    Handle hoBd
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  Demonstrates the GetAlternativeBodyBd method, which copies the contents of the Nth
    //  alternative body into a BinData object.  The first alternative body is at index 0; call
    //  only when NumAlternatives is greater than 0.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "GetAlternativeBodyBd example"

    Send ComSetTextBody To hoEmail "This is the plain-text alternative." "text/plain"
    Get ComAddHtmlAlternativeBody Of hoEmail "<html><body>This is the HTML alternative.</body></html>" To iSuccess

    //  Copy the first alternative body (index 0) into a BinData object.
    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End

    Get pvComObject of hoBd to vBd
    Get ComGetAlternativeBodyBd Of hoEmail 0 vBd To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumBytes Of hoBd To iTemp1
    Showln "Alternative 0 size (bytes) = " iTemp1


End_Procedure