PureBasic
PureBasic
How to Download Messages in MessageSet One at a Time
See more IMAP Examples
If a message set contains a huge number of emails, it's NOT a good idea to try to download all at once into an email bundle using a method such as FetchBundle. It's better to iterate over the messages in the set to download one by one.Chilkat PureBasic Downloads
IncludeFile "CkImap.pb"
IncludeFile "CkMessageSet.pb"
IncludeFile "CkEmail.pb"
Procedure ChilkatExample()
success.i = 0
; This example requires 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 using 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
; Authenticate
success = CkImap::ckLogin(imap,"email_account_login","email_account_password")
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
; Search for messages and return a set of matching messages.
; (This example will simply search for ALL messages.)
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
Debug "Number of messages = " + Str(CkMessageSet::ckCount(messageSet))
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
i.i = 0
While i < CkMessageSet::ckCount(messageSet)
success = CkImap::ckFetchEmail(imap,0,CkMessageSet::ckGetId(messageSet,i),fetchUids,email)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(messageSet)
CkEmail::ckDispose(email)
ProcedureReturn
EndIf
Debug CkEmail::ckFrom(email) + "; " + CkEmail::ckSubject(email)
i = i + 1
Wend
Debug "OK"
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(messageSet)
CkEmail::ckDispose(email)
ProcedureReturn
EndProcedure