Sample code for 30+ languages & platforms
Xbase++

Fetch the Flags Set on an IMAP Message

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchFlags method, which returns the flags currently set on a single message as a space-separated string. The first argument is the message ID and the second (bUid) selects UID vs sequence number. This example fetches and prints the flags for one message identified by UID.

Background: The returned string is the server's current flag list for the message, such as \Seen \Flagged. This is a quick way to check a single message's state — for example, to decide whether it still needs processing — without downloading the message itself. To read the same information from a message you already have as an Email object, use GetMailFlag instead.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oMsgSet
LOCAL nUid
LOCAL cFlags

nSuccess := 0

//  Demonstrates the Imap.FetchFlags method, which returns the flags currently set on a single
//  message as a space-separated string.  The 1st argument is the message ID and the 2nd (bUid)
//  selects UID vs sequence number.

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("ALL", 1, oMsgSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMsgSet:destroy()
    RETURN
ENDIF

IF (oMsgSet:Count > 0)
    nUid := oMsgSet:GetId(0)
    //  FetchFlags returns a string, so assign it to a string variable and check for success.
    cFlags := oImap:FetchFlags(nUid, 1)
    IF (oImap:LastMethodSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oMsgSet:destroy()
        RETURN
    ENDIF

    ? "UID " + Str(nUid) + " flags: " + cFlags
ENDIF

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

oImap:destroy()
oMsgSet:destroy()