PureBasic
PureBasic
Set a Single Flag on One IMAP Message
See more IMAP Examples
Demonstrates the Chilkat Imap.SetFlag method, which sets or clears a single flag on one message identified by ID. The first argument is the message ID, the second (bUid) selects UID vs sequence number, the third is the flag name, and the fourth is the value (1 to set, 0 to clear). This example flags one message as Deleted and then calls Expunge to permanently remove it.
Background: Deleting an IMAP message is a two-step process by design: setting the
Deleted flag only marks the message, and Expunge is what actually removes every message so marked. This separation lets a client show "deleted" items and offer an undo before the change becomes permanent. SetFlag handles one flag on one message; use StoreFlags to change several flags at once, or SetFlags to update a whole MessageSet.Chilkat PureBasic Downloads
IncludeFile "CkImap.pb"
IncludeFile "CkMessageSet.pb"
Procedure ChilkatExample()
success.i = 0
; Demonstrates the Imap.SetFlag method, which sets or clears a single flag on one message
; identified by ID. The 1st argument is the message ID, the 2nd (bUid) selects UID vs
; sequence number, the 3rd is the flag name, and the 4th is the value (1 to set, 0 to clear).
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
success = CkImap::ckSelectMailbox(imap,"Inbox")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
msgSet.i = CkMessageSet::ckCreate()
If msgSet.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkImap::ckQueryMbx(imap,"ALL",1,msgSet)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(msgSet)
ProcedureReturn
EndIf
If CkMessageSet::ckCount(msgSet) > 0
; Mark the first message for deletion. The "Deleted" flag is later acted upon by Expunge.
uid.i = CkMessageSet::ckGetId(msgSet,0)
success = CkImap::ckSetFlag(imap,uid,1,"Deleted",1)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(msgSet)
ProcedureReturn
EndIf
; Permanently remove all messages flagged as Deleted.
success = CkImap::ckExpunge(imap)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(msgSet)
ProcedureReturn
EndIf
Debug "Deleted UID " + Str(uid)
EndIf
success = CkImap::ckDisconnect(imap)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(msgSet)
ProcedureReturn
EndIf
CkImap::ckDispose(imap)
CkMessageSet::ckDispose(msgSet)
ProcedureReturn
EndProcedure