Xbase++
Xbase++
Set an IMAP Flag Using an Email Object
See more IMAP Examples
Demonstrates the Chilkat Imap.SetMailFlag method, which sets or clears a flag on the server for the message represented by an Email object. The first argument is the Email, the second is the flag name, and the third is the value (1 to set, 0 to clear). This example flags each fetched message as Flagged on the server.
Background:
SetMailFlag is convenient when you already hold an Email from a fetch: Chilkat uses the UID it recorded on that object to target the right message on the server, so you do not have to track IDs yourself. It updates both the server and the local Email object, keeping the two in sync. Under the hood this is the same STORE operation as SetFlag — just addressed by object rather than by raw ID.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oImap
LOCAL oMsgSet
LOCAL oBundle
LOCAL oEmail
LOCAL i
nSuccess := 0
// Demonstrates the Imap.SetMailFlag method, which sets or clears a flag on the server for the
// message represented by an Email object. The 1st argument is the Email, the 2nd is the flag
// name, and the 3rd is the value (1 to set, 0 to clear).
oImap := CreateObject("Chilkat.Imap")
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
nSuccess := oImap:Login("user@example.com", "myPassword")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
RETURN
ENDIF
oMsgSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("UNSEEN", 1, oMsgSet)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMsgSet:destroy()
RETURN
ENDIF
oBundle := CreateObject("Chilkat.EmailBundle")
nSuccess := oImap:FetchMsgSet(1, oMsgSet, oBundle)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMsgSet:destroy()
oBundle:destroy()
RETURN
ENDIF
oEmail := CreateObject("Chilkat.Email")
FOR i := 0 TO oBundle:MessageCount - 1
nSuccess := oBundle:EmailAt(i, oEmail)
// Mark this message as Flagged on the server. A value of 1 sets the flag; 0 would clear it.
nSuccess := oImap:SetMailFlag(oEmail, "Flagged", 1)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMsgSet:destroy()
oBundle:destroy()
oEmail:destroy()
RETURN
ENDIF
NEXT
? "Flagged " + Str(oBundle:MessageCount) + " messages."
nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oMsgSet:destroy()
oBundle:destroy()
oEmail:destroy()
RETURN
ENDIF
oImap:destroy()
oMsgSet:destroy()
oBundle:destroy()
oEmail:destroy()