Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 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).

set imap [new_CkImap]

CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993

set success [CkImap_Connect $imap "imap.example.com"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set success [CkImap_Login $imap "user@example.com" "myPassword"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set success [CkImap_SelectMailbox $imap "Inbox"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

#  Get the UIDs of all messages.
set msgSet [new_CkMessageSet]

set success [CkImap_QueryMbx $imap "ALL" 1 $msgSet]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $msgSet
    exit
}

if {[CkMessageSet_get_Count $msgSet] > 0} then {
    #  Apply the Seen and Flagged flags to the first message.  bUid is 1 because the ID
    #  came from a UID search.
    set uid [CkMessageSet_GetId $msgSet 0]
    set success [CkImap_StoreFlags $imap $uid 1 "Seen Flagged" 1]
    if {$success == 0} then {
        puts [CkImap_lastErrorText $imap]
        delete_CkImap $imap
        delete_CkMessageSet $msgSet
        exit
    }

    puts "Updated flags on UID $uid"
}

set success [CkImap_Disconnect $imap]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $msgSet
    exit
}


delete_CkImap $imap
delete_CkMessageSet $msgSet