Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.0.0+

Read an attached email

See more Email Object Examples

Open forward-as-attachment.eml from the forwarding example. Read the cover note’s subject, then open each attached message and inspect its own headers and attachment count. This works even when the outer message has no ordinary files.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

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

    success = CkEmail::ckLoadEml(wrapper,"forward-as-attachment.eml")
    If success <> 1
        Debug CkEmail::ckLastErrorText(wrapper)
        CkEmail::ckDispose(wrapper)
        ProcedureReturn
    EndIf

    Debug "Cover note: " + CkEmail::ckSubject(wrapper)
    If CkEmail::ckNumAttachedMessages(wrapper) = 0
        Debug "No attached messages."
        CkEmail::ckDispose(wrapper)
        ProcedureReturn
    EndIf

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

    i.i = 0
    While (i < CkEmail::ckNumAttachedMessages(wrapper))
        success = CkEmail::ckGetAttachedEmail(wrapper,i,nested)
        If success <> 1
            Debug CkEmail::ckLastErrorText(wrapper)
            CkEmail::ckDispose(wrapper)
            CkEmail::ckDispose(nested)
            ProcedureReturn
        EndIf

        Debug "Original author: " + CkEmail::ckFrom(nested)
        Debug "Original subject: " + CkEmail::ckSubject(nested)
        Debug "Files on the original: " + Str(CkEmail::ckNumAttachments(nested))
        i = i + 1
    Wend


    CkEmail::ckDispose(wrapper)
    CkEmail::ckDispose(nested)


    ProcedureReturn
EndProcedure