Sample code for 30+ languages & platforms
Lianja

How to Download Messages in MessageSet One at a Time

See more IMAP Examples

If a message set contains a huge number of emails, it's NOT a good idea to try to download all at once into an email bundle using a method such as FetchBundle. It's better to iterate over the messages in the set to download one by one.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

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

loImap = createobject("CkImap")

// Connect using TLS.
loImap.Ssl = .T.
loImap.Port = 993
llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Authenticate
llSuccess = loImap.Login("email_account_login","email_account_password")
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

// Search for messages and return a set of matching messages.
// (This example will simply search for ALL messages.)
llFetchUids = .T.

loMessageSet = createobject("CkMessageSet")
llSuccess = loImap.QueryMbx("ALL",llFetchUids,loMessageSet)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMessageSet
    return
endif

? "Number of messages = " + str(loMessageSet.Count)

loEmail = createobject("CkEmail")
i = 0
do while i < loMessageSet.Count
    llSuccess = loImap.FetchEmail(.F.,loMessageSet.GetId(i),llFetchUids,loEmail)
    if (llSuccess = .F.) then
        ? loImap.LastErrorText
        release loImap
        release loMessageSet
        release loEmail
        return
    endif

    ? loEmail.From + "; " + loEmail.Subject

    i = i + 1
enddo

? "OK"


release loImap
release loMessageSet
release loEmail