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

Sorting Email

Demonstrates how to sort an email bundle.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nFetchUids
LOCAL oMessageSet
LOCAL oBundle
LOCAL nHeadersOnly
LOCAL nAscending
LOCAL oEmail
LOCAL i

nSuccess := 0

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

oImap := CreateObject("Chilkat.Imap")

//  Connect to the IMAP server.
//  Use TLS
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Login
nSuccess := oImap:Login("myLogin", "myPassword")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nFetchUids := 1
oMessageSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("ALL", nFetchUids, oMessageSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

//  Fetch the email headers into a bundle object:
oBundle := CreateObject("Chilkat.EmailBundle")
nHeadersOnly := 1
nSuccess := oImap:FetchMsgSet(nHeadersOnly, oMessageSet, oBundle)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    oBundle:destroy()
    RETURN
ENDIF

//  Sort the email bundle by date, recipient, sender, or subject:
nAscending := 1
oBundle:SortByDate(nAscending)

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

//  Display the Subject and From of each email.
oEmail := CreateObject("Chilkat.Email")
i := 0
DO WHILE i < oBundle:MessageCount
    oBundle:EmailAt(i, oEmail)

    ? oEmail:GetHeaderField("Date")
    ? oEmail:Subject
    ? oEmail:From
    ? "--"

    i := i + 1
ENDDO

//  Disconnect from the IMAP server.
nSuccess := oImap:Disconnect()

oImap:destroy()
oMessageSet:destroy()
oBundle:destroy()
oEmail:destroy()