Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loImap
LOCAL loMsgSet
LOCAL loBundle
LOCAL loEmail
LOCAL i

lnSuccess = 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).

loImap = CreateObject('Chilkat.Imap')

loImap.Ssl = 1
loImap.Port = 993

lnSuccess = loImap.Connect("imap.example.com")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.Login("user@example.com","myPassword")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.SelectMailbox("Inbox")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

loMsgSet = CreateObject('Chilkat.MessageSet')
lnSuccess = loImap.QueryMbx("UNSEEN",1,loMsgSet)
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMsgSet
    CANCEL
ENDIF

loBundle = CreateObject('Chilkat.EmailBundle')
lnSuccess = loImap.FetchMsgSet(1,loMsgSet,loBundle)
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMsgSet
    RELEASE loBundle
    CANCEL
ENDIF

loEmail = CreateObject('Chilkat.Email')

FOR i = 0 TO loBundle.MessageCount - 1
    lnSuccess = loBundle.EmailAt(i,loEmail)
    *  Mark this message as Flagged on the server.  A value of 1 sets the flag; 0 would clear it.
    lnSuccess = loImap.SetMailFlag(loEmail,"Flagged",1)
    IF (lnSuccess = 0) THEN
        ? loImap.LastErrorText
        RELEASE loImap
        RELEASE loMsgSet
        RELEASE loBundle
        RELEASE loEmail
        CANCEL
    ENDIF

NEXT

? "Flagged " + STR(loBundle.MessageCount) + " messages."

lnSuccess = loImap.Disconnect()
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMsgSet
    RELEASE loBundle
    RELEASE loEmail
    CANCEL
ENDIF

RELEASE loImap
RELEASE loMsgSet
RELEASE loBundle
RELEASE loEmail