DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoImap
Integer iNumMessages
Integer iStartSeqnum
Variant vBundle
Handle hoBundle
Integer n
String sTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatImap)) To hoImap
If (Not(IsComObjectCreated(hoImap))) Begin
Send CreateComObject of hoImap
End
Set ComSsl Of hoImap To True
Set ComPort Of hoImap To 993
Get ComConnect Of hoImap "imap.example.com" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComLogin Of hoImap "user@example.com" "myPassword" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// SelectMailbox returns the number of messages in the mailbox, or -1 on failure.
Get ComSelectMailbox Of hoImap "Inbox" To iNumMessages
If (iNumMessages < 0) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// Download the 10 most recent messages (headers only).
Move (iNumMessages - 9) To iStartSeqnum
If (iStartSeqnum < 1) Begin
Move 1 To iStartSeqnum
End
Get Create (RefClass(cComChilkatEmailBundle)) To hoBundle
If (Not(IsComObjectCreated(hoBundle))) Begin
Send CreateComObject of hoBundle
End
Get pvComObject of hoBundle to vBundle
Get ComFetchRange Of hoImap True iStartSeqnum 10 vBundle To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComMessageCount Of hoBundle To n
Showln "Fetched " n " message headers."
Get ComDisconnect Of hoImap To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
End_Procedure