Sample code for 30+ languages & platforms
PureBasic

Sorting Email

Demonstrates how to sort an email bundle.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkImap.pb"
IncludeFile "CkEmailBundle.pb"
IncludeFile "CkMessageSet.pb"
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

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

    ; Connect to the IMAP server.
    ; Use TLS
    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

    ; Login
    success = CkImap::ckLogin(imap,"myLogin","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Select an IMAP mailbox
    success = CkImap::ckSelectMailbox(imap,"Inbox")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    fetchUids.i = 1
    messageSet.i = CkMessageSet::ckCreate()
    If messageSet.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckQueryMbx(imap,"ALL",fetchUids,messageSet)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(messageSet)
        ProcedureReturn
    EndIf

    ; Fetch the email headers into a bundle object:
    bundle.i = CkEmailBundle::ckCreate()
    If bundle.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    headersOnly.i = 1
    success = CkImap::ckFetchMsgSet(imap,headersOnly,messageSet,bundle)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(messageSet)
        CkEmailBundle::ckDispose(bundle)
        ProcedureReturn
    EndIf

    ; Sort the email bundle by date, recipient, sender, or subject:
    ascending.i = 1
    CkEmailBundle::ckSortByDate(bundle,ascending)

    ; To sort by recipient, sender, or subject, call 
    ; SortBySender, SortByRecipient, or SortBySubject.

    ; Display the Subject and From of each email.
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i = 0
    While i < CkEmailBundle::ckMessageCount(bundle)
        CkEmailBundle::ckEmailAt(bundle,i,email)

        Debug CkEmail::ckGetHeaderField(email,"Date")
        Debug CkEmail::ckSubject(email)
        Debug CkEmail::ckFrom(email)
        Debug "--"

        i = i + 1
    Wend

    ; Disconnect from the IMAP server.
    success = CkImap::ckDisconnect(imap)


    CkImap::ckDispose(imap)
    CkMessageSet::ckDispose(messageSet)
    CkEmailBundle::ckDispose(bundle)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure