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

Fetch Oldest/Newest IMAP Email

Emails may be downloaded by sequence number. Assuming the selected mailbox is not empty, the oldest email is at sequence number 1, and the newest email is at sequence number N. The FetchSingle method may be used to download by sequence number.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL n
LOCAL nIsUid
LOCAL oOldestEmail
LOCAL oNewestEmail

nSuccess := 0

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

oImap := CreateObject("Chilkat.Imap")

//  Connect to an IMAP server.
//  Use TLS
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("mail.testemail.net")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Login
nSuccess := oImap:Login("***", "***")
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

//  After selecting a mailbox, the NumMessages property
//  contains the number of emails in the selected mailbox.

n := oImap:NumMessages

IF (n > 0)

    //  The oldest email is always at sequence number 1.
    nIsUid := 0

    oOldestEmail := CreateObject("Chilkat.Email")
    nSuccess := oImap:FetchEmail(0, 1, nIsUid, oOldestEmail)
    IF (nSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oOldestEmail:destroy()
        RETURN
    ENDIF

    //  Display the From and Subject
    ? oOldestEmail:FromAddress
    ? oOldestEmail:Subject

    ? "--"

    //  The newest email is at sequence number N:
    oNewestEmail := CreateObject("Chilkat.Email")
    nSuccess := oImap:FetchEmail(0, n, nIsUid, oNewestEmail)
    IF (nSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oOldestEmail:destroy()
        oNewestEmail:destroy()
        RETURN
    ENDIF

    //  Display the From and Subject
    ? oNewestEmail:FromAddress
    ? oNewestEmail:Subject

ENDIF

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

oImap:destroy()
oOldestEmail:destroy()
oNewestEmail:destroy()