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

Read email headers and display the body as text

See more Email Object Examples

Read the sender, recipients, subject, date, and an optional custom header from an EML file. Display the plain-text body when available, or convert HTML to text. Handle messages that contain neither body format.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkEmail.pb"
IncludeFile "CkHtmlToText.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

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

    success = CkEmail::ckLoadEml(email,"with-attachments.eml")
    If success <> 1
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    Debug "From: " + CkEmail::ckFrom(email)
    Debug "Subject: " + CkEmail::ckSubject(email)
    Debug "Date: " + CkEmail::ckEmailDateStr(email)
    i.i = 0
    While (i < CkEmail::ckNumTo(email))
        Debug "To: " + CkEmail::ckGetToName(email,i) + "  " + CkEmail::ckGetToAddr(email,i)
        i = i + 1
    Wend
    referenceId.s = CkEmail::ckGetHeaderField(email,"X-Reference-ID")
    If CkEmail::ckLastMethodSuccess(email) = 1
        Debug "Reference: " + referenceId
    EndIf

    If CkEmail::ckHasPlainTextBody(email) = 1
        Debug CkEmail::ckGetPlainTextBody(email)
    Else
        If CkEmail::ckHasHtmlBody(email) = 1
            reader.i = CkHtmlToText::ckCreate()
            If reader.i = 0
                Debug "Failed to create object."
                ProcedureReturn
            EndIf

            Debug CkHtmlToText::ckToText(reader,CkEmail::ckGetHtmlBody(email))
        Else
            Debug "No text or HTML body."
        EndIf

    EndIf



    CkEmail::ckDispose(email)
    CkHtmlToText::ckDispose(reader)


    ProcedureReturn
EndProcedure