PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkEmailBundle.pb"
IncludeFile "CkImap.pb"
Procedure ChilkatExample()
success.i = 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.
imap.i = CkImap::ckCreate()
If imap.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkImap::setCkSsl(imap, 1)
CkImap::setCkPort(imap, 993)
success = CkImap::ckConnect(imap,"imap.example.com")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
success = CkImap::ckLogin(imap,"user@example.com","myPassword")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
; SelectMailbox returns the number of messages in the mailbox, or -1 on failure.
numMessages.i = CkImap::ckSelectMailbox(imap,"Inbox")
If numMessages < 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
; Download the 10 most recent messages (headers only).
startSeqnum.i = numMessages - 9
If startSeqnum < 1
startSeqnum = 1
EndIf
bundle.i = CkEmailBundle::ckCreate()
If bundle.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkImap::ckFetchRange(imap,1,startSeqnum,10,bundle)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkEmailBundle::ckDispose(bundle)
ProcedureReturn
EndIf
n.i = CkEmailBundle::ckMessageCount(bundle)
Debug "Fetched " + Str(n) + " message headers."
success = CkImap::ckDisconnect(imap)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkEmailBundle::ckDispose(bundle)
ProcedureReturn
EndIf
CkImap::ckDispose(imap)
CkEmailBundle::ckDispose(bundle)
ProcedureReturn
EndProcedure