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

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nFetchUids
LOCAL oMessageSet
LOCAL nNumFound
LOCAL nUpperBound
LOCAL i
LOCAL nBUid
LOCAL nHeaderOnly
LOCAL oEmail

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

//  Get the message IDs of all the emails in the mailbox
//  We can choose to fetch UIDs or sequence numbers.
nFetchUids := 1
oMessageSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("ALL", nFetchUids, oMessageSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

nNumFound := oMessageSet:Count
IF (nNumFound == 0)
    ? "No messages found."
    oImap:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

//  Get the 1st 10 messages in messageSet.
nUpperBound := 10
IF (nNumFound < nUpperBound)
    nUpperBound := nNumFound
ENDIF

i := 0
nBUid := oMessageSet:HasUids
nHeaderOnly := 1
oEmail := CreateObject("Chilkat.Email")

DO WHILE i < nUpperBound

    nSuccess := oImap:FetchEmail(nHeaderOnly, oMessageSet:GetId(i), nBUid, oEmail)
    IF (nSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oMessageSet:destroy()
        oEmail:destroy()
        RETURN
    ENDIF

    ? Str(i) + ": " + oEmail:Subject

    i := i + 1
ENDDO

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

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