Sample code for 30+ languages & platforms
PureBasic

Get IMAP Message Threads as JSON

See more IMAP Examples

Demonstrates the Chilkat Imap.QueryThread method, which sends the IMAP THREAD command for the selected mailbox and returns the thread hierarchy as JSON. The first argument is the threading algorithm (commonly REFERENCES or ORDEREDSUBJECT), the second is IMAP search criteria, and the third (bUid) selects UID vs sequence-number identifiers. The returned JSON has a top-level threads array whose nested arrays encode parent/child relationships. This example threads all messages in the Inbox.

Tip: JSON parsing code for this result can be generated at Chilkat Tools.

Background: "Conversation view" in a mail client — grouping a message with its replies and follow-ups — is exactly what the IMAP THREAD extension provides, computed on the server. REFERENCES threads by the References/In-Reply-To headers (the accurate way), while ORDEREDSUBJECT groups by subject. Not all servers support THREAD, so check HasCapability for it first.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkImap.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Imap.QueryThread method, which sends the IMAP THREAD command for the
    ;  selected mailbox and returns the thread hierarchy as JSON.  The 1st argument is the
    ;  threading algorithm (such as REFERENCES or ORDEREDSUBJECT), the 2nd is IMAP search
    ;  criteria, and the 3rd (bUid) selects UID vs sequence-number identifiers.

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

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)

    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"user@example.com","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckSelectMailbox(imap,"Inbox")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  Group all messages into threads using the REFERENCES algorithm, returning UIDs.
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckQueryThread(imap,"REFERENCES","ALL",1,json)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(json, 0)
    Debug CkJsonObject::ckEmit(json)

    ;  JSON parsing code for this result can be generated at Chilkat's online tool:
    ;  https://tools.chilkat.io/jsonParse

    success = CkImap::ckDisconnect(imap)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf



    CkImap::ckDispose(imap)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure