Sample code for 30+ languages & platforms
Lianja

Fetch a Chunk of Messages, Tracking Failures

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchChunk2 method, which downloads a run of messages by sequence number and reports which succeeded and which failed. The arguments are the starting sequence number, the count, a MessageSet that receives the failed IDs, a MessageSet that receives the successfully-fetched IDs, and the EmailBundle. This example fetches the first chunk of messages and reports the success and failure counts.

Background: When bulk-downloading a large mailbox, individual messages can occasionally fail to fetch (a corrupt message on the server, a transient error) without failing the whole operation. FetchChunk2 is designed for that reality: it returns a partial EmailBundle plus two MessageSet objects so your code can log or retry exactly the messages that did not come through, rather than aborting the entire sync.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

//  Demonstrates the Imap.FetchChunk2 method, which downloads a run of messages by sequence
//  number and reports which succeeded and which failed.  The 1st argument is the starting
//  sequence number, the 2nd is the count, the 3rd receives the failed IDs, the 4th receives
//  the successfully-fetched IDs, and the 5th is the EmailBundle.

loImap = createobject("CkImap")

loImap.Ssl = .T.
loImap.Port = 993

llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llSuccess = loImap.Login("user@example.com","myPassword")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

lnNumMessages = loImap.SelectMailbox("Inbox")
if (lnNumMessages < 0) then
    ? loImap.LastErrorText
    release loImap
    return
endif

//  Fetch messages 1..25 (or fewer if the mailbox is smaller).
lnCount = 25
if (lnCount > lnNumMessages) then
    lnCount = lnNumMessages
endif

loFailedSet = createobject("CkMessageSet")
loFetchedSet = createobject("CkMessageSet")
loBundle = createobject("CkEmailBundle")
llSuccess = loImap.FetchChunk2(1,lnCount,loFailedSet,loFetchedSet,loBundle)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loFailedSet
    release loFetchedSet
    release loBundle
    return
endif

? "Fetched: " + str(loFetchedSet.Count)
? "Failed:  " + str(loFailedSet.Count)

llSuccess = loImap.Disconnect()
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loFailedSet
    release loFetchedSet
    release loBundle
    return
endif



release loImap
release loFailedSet
release loFetchedSet
release loBundle