PureBasic
PureBasic
Delete Email Individually (One at a time) from an IMAP Mailbox
Downloads email from an IMAP mailbox and deletes emails individually (one by one).Chilkat PureBasic Downloads
IncludeFile "CkImap.pb"
IncludeFile "CkEmailBundle.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,"myLogin","myPassword")
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
; We can choose to fetch UIDs or sequence numbers.
fetchUids.i = 1
; Get the message IDs of all the emails in the mailbox
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
; Fetch the emails into a bundle object:
bundle.i = CkEmailBundle::ckCreate()
If bundle.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
headersOnly.i = 0
success = CkImap::ckFetchMsgSet(imap,headersOnly,messageSet,bundle)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(messageSet)
CkEmailBundle::ckDispose(bundle)
ProcedureReturn
EndIf
; To mark a complete set of emails for deletion, call SetFlags:
success = CkImap::ckSetFlags(imap,messageSet,"Deleted",1)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(messageSet)
CkEmailBundle::ckDispose(bundle)
ProcedureReturn
EndIf
; Messages can also be marked for deletion individually:
; Loop over the bundle and mark each message for deletion.
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
i.i = 0
numEmails.i = CkEmailBundle::ckMessageCount(bundle)
While i < numEmails
CkEmailBundle::ckEmailAt(bundle,i,email)
Debug CkEmail::ckFrom(email)
Debug CkEmail::ckSubject(email)
; To delete this email, set the "Deleted" flag to 1.
; The email is not actually deleted until Expunge or
; ExpungeAndClose is called.
success = CkImap::ckSetMailFlag(imap,email,"Deleted",1)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(messageSet)
CkEmailBundle::ckDispose(bundle)
CkEmail::ckDispose(email)
ProcedureReturn
EndIf
Debug "--"
i = i + 1
Wend
success = CkImap::ckExpungeAndClose(imap)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(messageSet)
CkEmailBundle::ckDispose(bundle)
CkEmail::ckDispose(email)
ProcedureReturn
EndIf
; Disconnect from the IMAP server.
success = CkImap::ckDisconnect(imap)
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(messageSet)
CkEmailBundle::ckDispose(bundle)
CkEmail::ckDispose(email)
ProcedureReturn
EndProcedure