Sample code for 30+ languages & platforms
Tcl

Get an IMAP Flag from a Downloaded Email

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailFlag method, which returns the state of a named flag for an Email object that was downloaded from the server. The first argument is the Email and the second is the flag name. The return value is 1 if the flag is set, 0 if not, and -1 if the flag is not present. This example iterates a bundle and reports which messages are read vs unread.

Background: When Chilkat downloads a message it records the flags the server reported at fetch time, so you can inspect them locally with GetMailFlag without another round trip. This is the client-side view: it reflects the state as of the download, not necessarily the live state on the server, which other clients could have changed. Use RefetchMailFlags to update an Email object's flags to the current server values.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the Imap.GetMailFlag method, which returns the state of a named flag for an
#  Email object that was downloaded from the server.  The 1st argument is the Email and the
#  2nd is the flag name.  The return value is 1 if the flag is set, 0 if not, and -1 if the
#  flag is not present.

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]

for {set i 0} {$i <= [expr [CkEmailBundle_get_MessageCount $bundle] - 1]} {incr i} {
    set success [CkEmailBundle_EmailAt $bundle $i $email]
    #  Check whether each message has been read (the "Seen" flag).
    set seen [CkImap_GetMailFlag $imap $email "Seen"]
    if {$seen == 1} then {
        puts [CkEmail_subject $email] -- already read
    }

ERROR: "}" expected
}

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