Sample code for 30+ languages & platforms
Xbase++

Set or Clear a Flag on a MessageSet

See more IMAP Examples

Demonstrates the Chilkat Imap.SetFlags method, which sets or clears a flag on every message in a MessageSet. The first argument is the MessageSet, the second is the flag name, and the third is the value (1 to set the flag, 0 to clear it). This example marks all unseen messages as Seen.

Background: IMAP flags are per-message keywords the server stores and shares across all clients. The standard system flags are Seen, Answered, Flagged, Deleted, Draft, and Recent. Give the flag name without the leading backslash here (use "Seen", not "\Seen"). SetFlags applies the change to the whole set in one round trip, which is how a client implements "mark all as read."

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oMsgSet

nSuccess := 0

//  Demonstrates the Imap.SetFlags method, which sets or clears a flag on every message in a
//  MessageSet.  The 1st argument is the MessageSet, the 2nd is the flag name, and the 3rd is
//  the value (1 to set the flag, 0 to clear it).

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

//  Find the messages to flag (here, all unseen messages, by UID).
oMsgSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("UNSEEN", 1, oMsgSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMsgSet:destroy()
    RETURN
ENDIF

//  Mark all of them as seen.  A value of 1 sets the flag; 0 would clear it.
nSuccess := oImap:SetFlags(oMsgSet, "Seen", 1)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMsgSet:destroy()
    RETURN
ENDIF

? "Marked " + Str(oMsgSet:Count) + " messages as Seen."

nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMsgSet:destroy()
    RETURN
ENDIF

oImap:destroy()
oMsgSet:destroy()