Sample code for 30+ languages & platforms
Lianja

Fetch 1st N Headers of Search Results

Calls Search to get a message set, then downloads the 1st N messages in the message set. There are two equivalent ways of doing it: (1) iterate from 0 to N-1 and download each message individually, or (2) create a new message set that contains the 1st N messages and pass it to FetchHeaders. Both are demonstrated here.

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("imap.example.com")
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

// Get the message IDs of all the emails in the mailbox
// We can choose to fetch UIDs or sequence numbers.
llFetchUids = .T.
loMessageSet = createobject("CkMessageSet")
llSuccess = loImap.QueryMbx("ALL",llFetchUids,loMessageSet)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMessageSet
    return
endif

lnNumFound = loMessageSet.Count
if (lnNumFound = 0) then
    ? "No messages found."
    release loImap
    release loMessageSet
    return
endif

// Get the 1st 10 messages in messageSet.
lnUpperBound = 10
if (lnNumFound < lnUpperBound) then
    lnUpperBound = lnNumFound
endif

i = 0
llBUid = loMessageSet.HasUids
llHeaderOnly = .T.
loEmail = createobject("CkEmail")

do while i < lnUpperBound

    llSuccess = loImap.FetchEmail(llHeaderOnly,loMessageSet.GetId(i),llBUid,loEmail)
    if (llSuccess = .F.) then
        ? loImap.LastErrorText
        release loImap
        release loMessageSet
        release loEmail
        return
    endif

    ? str(i) + ": " + loEmail.Subject

    i = i + 1
enddo

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


release loImap
release loMessageSet
release loEmail