Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

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

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

    CkEmail::setCkSubject(email, "GetAlternativeBodyBd example")

    CkEmail::ckSetTextBody(email,"This is the plain-text alternative.","text/plain")
    CkEmail::ckAddHtmlAlternativeBody(email,"<html><body>This is the HTML alternative.</body></html>")

    ;  Copy the first alternative body (index 0) into a BinData object.
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkEmail::ckGetAlternativeBodyBd(email,0,bd)
    If success = 0
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    Debug "Alternative 0 size (bytes) = " + Str(CkBinData::ckNumBytes(bd))


    CkEmail::ckDispose(email)
    CkBinData::ckDispose(bd)


    ProcedureReturn
EndProcedure