Sample code for 30+ languages & platforms
PureBasic

Store One or More Flags on a Single Message

See more IMAP Examples

Demonstrates the Chilkat Imap.StoreFlags method, which sets or clears one or more flags on a single message identified by ID. The first argument is the message ID, the second (bUid) selects UID vs sequence number, the third is a space-separated list of flag names, and the fourth is the value (1 to set, 0 to clear). This example applies the Seen and Flagged flags to one message by UID.

Background: StoreFlags is the single-message counterpart to SetFlags and lets you change several flags at once by listing them space-separated (without leading backslashes). Because the ID can be either a UID or a sequence number, the bUid argument must match how you obtained it — a UID from a UID search, or a sequence number from a sequence-based operation. UIDs are the safer choice since they remain valid as the mailbox changes.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkImap.pb"
IncludeFile "CkMessageSet.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Imap.StoreFlags method, which sets or clears one or more flags on a single
    ;  message identified by ID.  The 1st argument is the message ID, the 2nd (bUid) selects UID
    ;  vs sequence number, the 3rd is a space-separated list of flag names, and the 4th is the
    ;  value (1 to set, 0 to clear).

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)

    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"user@example.com","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckSelectMailbox(imap,"Inbox")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  Get the UIDs of all messages.
    msgSet.i = CkMessageSet::ckCreate()
    If msgSet.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckQueryMbx(imap,"ALL",1,msgSet)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(msgSet)
        ProcedureReturn
    EndIf

    If CkMessageSet::ckCount(msgSet) > 0
        ;  Apply the Seen and Flagged flags to the first message.  bUid is 1 because the ID
        ;  came from a UID search.
        uid.i = CkMessageSet::ckGetId(msgSet,0)
        success = CkImap::ckStoreFlags(imap,uid,1,"Seen Flagged",1)
        If success = 0
            Debug CkImap::ckLastErrorText(imap)
            CkImap::ckDispose(imap)
            CkMessageSet::ckDispose(msgSet)
            ProcedureReturn
        EndIf

        Debug "Updated flags on UID " + Str(uid)
    EndIf

    success = CkImap::ckDisconnect(imap)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(msgSet)
        ProcedureReturn
    EndIf



    CkImap::ckDispose(imap)
    CkMessageSet::ckDispose(msgSet)


    ProcedureReturn
EndProcedure