Sample code for 30+ languages & platforms
Xbase++

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

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nNumMessages
LOCAL nCount
LOCAL oFailedSet
LOCAL oFetchedSet
LOCAL oBundle

nSuccess := 0

//  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.

oImap := CreateObject("Chilkat.Imap")

oImap:Ssl := 1
oImap:Port := 993

nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:Login("user@example.com", "myPassword")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nNumMessages := oImap:SelectMailbox("Inbox")
IF (nNumMessages < 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Fetch messages 1..25 (or fewer if the mailbox is smaller).
nCount := 25
IF (nCount > nNumMessages)
    nCount := nNumMessages
ENDIF

oFailedSet := CreateObject("Chilkat.MessageSet")
oFetchedSet := CreateObject("Chilkat.MessageSet")
oBundle := CreateObject("Chilkat.EmailBundle")
nSuccess := oImap:FetchChunk2(1, nCount, oFailedSet, oFetchedSet, oBundle)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oFailedSet:destroy()
    oFetchedSet:destroy()
    oBundle:destroy()
    RETURN
ENDIF

? "Fetched: " + Str(oFetchedSet:Count)
? "Failed:  " + Str(oFailedSet:Count)

nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oFailedSet:destroy()
    oFetchedSet:destroy()
    oBundle:destroy()
    RETURN
ENDIF

oImap:destroy()
oFailedSet:destroy()
oFetchedSet:destroy()
oBundle:destroy()