Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loImap
LOCAL lnNumMessages
LOCAL lnCount
LOCAL loFailedSet
LOCAL loFetchedSet
LOCAL loBundle

lnSuccess = 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.

loImap = CreateObject('Chilkat.Imap')

loImap.Ssl = 1
loImap.Port = 993

lnSuccess = loImap.Connect("imap.example.com")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.Login("user@example.com","myPassword")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnNumMessages = loImap.SelectMailbox("Inbox")
IF (lnNumMessages < 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

*  Fetch messages 1..25 (or fewer if the mailbox is smaller).
lnCount = 25
IF (lnCount > lnNumMessages) THEN
    lnCount = lnNumMessages
ENDIF

loFailedSet = CreateObject('Chilkat.MessageSet')
loFetchedSet = CreateObject('Chilkat.MessageSet')
loBundle = CreateObject('Chilkat.EmailBundle')
lnSuccess = loImap.FetchChunk2(1,lnCount,loFailedSet,loFetchedSet,loBundle)
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loFailedSet
    RELEASE loFetchedSet
    RELEASE loBundle
    CANCEL
ENDIF

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

lnSuccess = loImap.Disconnect()
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loFailedSet
    RELEASE loFetchedSet
    RELEASE loBundle
    CANCEL
ENDIF

RELEASE loImap
RELEASE loFailedSet
RELEASE loFetchedSet
RELEASE loBundle