PureBasic
PureBasic
Fetch 1st N Headers of Search Results
Calls Search to get a message set, then downloads the 1st N messages in the message set. There are two equivalent ways of doing it: (1) iterate from 0 to N-1 and download each message individually, or (2) create a new message set that contains the 1st N messages and pass it to FetchHeaders. Both are demonstrated here.Chilkat PureBasic Downloads
IncludeFile "CkImap.pb"
IncludeFile "CkMessageSet.pb"
IncludeFile "CkEmail.pb"
Procedure ChilkatExample()
success.i = 0
; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
imap.i = CkImap::ckCreate()
If imap.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Connect to an IMAP server.
; Use TLS
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
; Login
success = CkImap::ckLogin(imap,"****","****")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
; Select an IMAP mailbox
success = CkImap::ckSelectMailbox(imap,"Inbox")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
; Get the message IDs of all the emails in the mailbox
; We can choose to fetch UIDs or sequence numbers.
fetchUids.i = 1
messageSet.i = CkMessageSet::ckCreate()
If messageSet.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkImap::ckQueryMbx(imap,"ALL",fetchUids,messageSet)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(messageSet)
ProcedureReturn
EndIf
numFound.i = CkMessageSet::ckCount(messageSet)
If numFound = 0
Debug "No messages found."
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(messageSet)
ProcedureReturn
EndIf
; Get the 1st 10 messages in messageSet.
upperBound.i = 10
If numFound < upperBound
upperBound = numFound
EndIf
i.i = 0
bUid.i = CkMessageSet::ckHasUids(messageSet)
headerOnly.i = 1
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
While i < upperBound
success = CkImap::ckFetchEmail(imap,headerOnly,CkMessageSet::ckGetId(messageSet,i),bUid,email)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(messageSet)
CkEmail::ckDispose(email)
ProcedureReturn
EndIf
Debug Str(i) + ": " + CkEmail::ckSubject(email)
i = i + 1
Wend
; Disconnect from the IMAP server.
success = CkImap::ckDisconnect(imap)
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(messageSet)
CkEmail::ckDispose(email)
ProcedureReturn
EndProcedure