Sample code for 30+ languages & platforms
PureBasic

Get an Email's MIME into a BinData

See more Email Object Examples

Demonstrates the Chilkat Email.GetMimeBd method, which serializes the complete RFC822/MIME message (headers, body representations, related items, and attachments) and appends the bytes to a BinData object. This example builds a message, serializes it to a BinData, and prints the byte count.

Background: A BinData holds raw bytes, which is the right container when you want the MIME as binary rather than text — for example to write it directly to a file or socket, hash it, or hand it to another API that expects a byte buffer. It is the binary counterpart to GetMime (string) and GetMimeSb (StringBuilder).

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the GetMimeBd method, which serializes the complete RFC822/MIME message
    ;  (headers, bodies, related items, and attachments) and appends the bytes to a BinData
    ;  object.

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

    CkEmail::setCkSubject(email, "GetMimeBd example")
    CkEmail::setCkFrom(email, "alice@example.com")
    CkEmail::ckAddTo(email,"Bob","bob@example.com")
    CkEmail::setCkBody(email, "Hello!")

    ;  Append the complete MIME to a BinData object.
    bdMime.i = CkBinData::ckCreate()
    If bdMime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    Debug "MIME size (bytes) = " + Str(CkBinData::ckNumBytes(bdMime))


    CkEmail::ckDispose(email)
    CkBinData::ckDispose(bdMime)


    ProcedureReturn
EndProcedure