Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

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

loImap = createobject("CkImap")

// Connect to an IMAP server.
// Use TLS
loImap.Ssl = .T.
loImap.Port = 993
llSuccess = loImap.Connect("mail.testemail.net")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Login
llSuccess = loImap.Login("***","***")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Select an IMAP mailbox
llSuccess = loImap.SelectMailbox("Inbox")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

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

n = loImap.NumMessages

if (n > 0) then

    // The oldest email is always at sequence number 1.
    llIsUid = .F.

    loOldestEmail = createobject("CkEmail")
    llSuccess = loImap.FetchEmail(.F.,1,llIsUid,loOldestEmail)
    if (llSuccess = .F.) then
        ? loImap.LastErrorText
        release loImap
        release loOldestEmail
        return
    endif

    // Display the From and Subject
    ? loOldestEmail.FromAddress
    ? loOldestEmail.Subject

    ? "--"

    // The newest email is at sequence number N:
    loNewestEmail = createobject("CkEmail")
    llSuccess = loImap.FetchEmail(.F.,n,llIsUid,loNewestEmail)
    if (llSuccess = .F.) then
        ? loImap.LastErrorText
        release loImap
        release loOldestEmail
        release loNewestEmail
        return
    endif

    // Display the From and Subject
    ? loNewestEmail.FromAddress
    ? loNewestEmail.Subject

endif

// Disconnect from the IMAP server.
llSuccess = loImap.Disconnect()


release loImap
release loOldestEmail
release loNewestEmail