Sample code for 30+ languages & platforms
Tcl

Refresh an Email Object's IMAP Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.RefetchMailFlags method, which updates the flags stored on an Email object to the current values on the server. The only argument is the Email object. This example re-reads the flags for a previously fetched message and then checks its Seen state.

Background: Flags are shared mailbox state, so between the time you fetch a message and the time you act on it, another client (a phone, a webmail tab) may have marked it read, flagged, or deleted. RefetchMailFlags pulls just the flags — not the whole message — so a long-running job can cheaply re-check the live state before deciding what to do, avoiding acting on stale information.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the Imap.RefetchMailFlags method, which updates the flags stored on an Email
#  object to the current values on the server.  The only argument is the Email object.

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 "ALL" 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]

if {[CkEmailBundle_get_MessageCount $bundle] > 0} then {
    set success [CkEmailBundle_EmailAt $bundle 0 $email]

    #  Re-read the flags from the server, in case another client changed them since the fetch.
    set success [CkImap_RefetchMailFlags $imap $email]
    if {$success == 0} then {
        puts [CkImap_lastErrorText $imap]
        delete_CkImap $imap
        delete_CkMessageSet $msgSet
        delete_CkEmailBundle $bundle
        delete_CkEmail $email
        exit
    }

    set seen [CkImap_GetMailFlag $imap $email "Seen"]
    puts "Current Seen state: $seen"
}

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