Xbase++
Xbase++
Fetch a Range of IMAP Messages by Sequence Number
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchRange method, which downloads a contiguous run of messages by sequence number. The arguments are headersOnly, the starting sequence number, the count of messages, and the EmailBundle that receives them. This example uses the message count returned by SelectMailbox to fetch the headers of the 10 most recent messages.
Background: IMAP sequence numbers are 1-based positions within the selected mailbox, from 1 (oldest) to the message count (newest), and they shift whenever messages are expunged. That makes
FetchRange ideal for "give me the newest N" style paging within a single session, but note that sequence numbers are not stable identifiers across sessions the way UIDs are — use a UID-based approach if you need to remember which messages you have already seen.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oImap
LOCAL nNumMessages
LOCAL nStartSeqnum
LOCAL oBundle
LOCAL n
nSuccess := 0
// Demonstrates the Imap.FetchRange method, which downloads a contiguous run of messages by
// sequence number. The 1st argument is headersOnly, the 2nd is the starting sequence number,
// the 3rd is the count of messages, and the 4th is the EmailBundle that receives them.
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
// SelectMailbox returns the number of messages in the mailbox, or -1 on failure.
nNumMessages := oImap:SelectMailbox("Inbox")
IF (nNumMessages < 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
// Download the 10 most recent messages (headers only).
nStartSeqnum := nNumMessages - 9
IF (nStartSeqnum < 1)
nStartSeqnum := 1
ENDIF
oBundle := CreateObject("Chilkat.EmailBundle")
nSuccess := oImap:FetchRange(1, nStartSeqnum, 10, oBundle)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oBundle:destroy()
RETURN
ENDIF
n := oBundle:MessageCount
? "Fetched " + Str(n) + " message headers."
nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oBundle:destroy()
RETURN
ENDIF
oImap:destroy()
oBundle:destroy()