Sample code for 30+ languages & platforms
Xbase++

Get an IMAP Flag from a Downloaded Email

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailFlag method, which returns the state of a named flag for an Email object that was downloaded from the server. The first argument is the Email and the second is the flag name. The return value is 1 if the flag is set, 0 if not, and -1 if the flag is not present. This example iterates a bundle and reports which messages are read vs unread.

Background: When Chilkat downloads a message it records the flags the server reported at fetch time, so you can inspect them locally with GetMailFlag without another round trip. This is the client-side view: it reflects the state as of the download, not necessarily the live state on the server, which other clients could have changed. Use RefetchMailFlags to update an Email object's flags to the current server values.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oMsgSet
LOCAL oBundle
LOCAL oEmail
LOCAL i
LOCAL nSeen

nSuccess := 0

//  Demonstrates the Imap.GetMailFlag method, which returns the state of a named flag for an
//  Email object that was downloaded from the server.  The 1st argument is the Email and the
//  2nd is the flag name.  The return value is 1 if the flag is set, 0 if not, and -1 if the
//  flag is not present.

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

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)
    //  Check whether each message has been read (the "Seen" flag).
    nSeen := oImap:GetMailFlag(oEmail, "Seen")
    IF (nSeen == 1)
        ? oEmail:Subject + " -- already read"
    ENDIF

ERROR: "}" expected
NEXT

oImap:destroy()
oMsgSet:destroy()
oBundle:destroy()
oEmail:destroy()