Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

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

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
}

set msgSet [new_CkMessageSet]

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

set bundle [new_CkEmailBundle]

set success [CkImap_FetchMsgSet $imap 1 $msgSet $bundle]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $msgSet
    delete_CkEmailBundle $bundle
    exit
}

set email [new_CkEmail]

for {set i 0} {$i <= [expr [CkEmailBundle_get_MessageCount $bundle] - 1]} {incr i} {
    set success [CkEmailBundle_EmailAt $bundle $i $email]
    #  Mark this message as Flagged on the server.  A value of 1 sets the flag; 0 would clear it.
    set success [CkImap_SetMailFlag $imap $email "Flagged" 1]
    if {$success == 0} then {
        puts [CkImap_lastErrorText $imap]
        delete_CkImap $imap
        delete_CkMessageSet $msgSet
        delete_CkEmailBundle $bundle
        delete_CkEmail $email
        exit
    }

}

puts "Flagged [CkEmailBundle_get_MessageCount $bundle] messages."

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


delete_CkImap $imap
delete_CkMessageSet $msgSet
delete_CkEmailBundle $bundle
delete_CkEmail $email