Sample code for 30+ languages & platforms
Tcl

Determine the Number of Unseen Email Messages

Demonstrates how to determine how many unseen messages exist in an email account on an IMAP server.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

set imap [new_CkImap]

# Connect to an IMAP server.
# Use TLS
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
}

# Login
set success [CkImap_Login $imap "***" "***"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

# Select an IMAP mailbox
set success [CkImap_SelectMailbox $imap "Inbox"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

# After selecting the mailbox. the total number of emails
# is immediately available:
set totalNum [CkImap_get_NumMessages $imap]
puts "Num messages = $totalNum"

# To determine the number of unseen messages, a call
# to Search is required, which returns the set of UIDs
# of all unseen messages.

# We can choose to fetch UIDs or sequence numbers.
set fetchUids 1
set messageSet [new_CkMessageSet]

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

set numUnseen [CkMessageSet_get_Count $messageSet]
puts "$numUnseen"

puts "UIDs ----"

# Display the UIDs
set i 0
while {$i < [CkMessageSet_get_Count $messageSet]} {
    puts [CkMessageSet_GetId $messageSet $i]
    set i [expr $i + 1]
}

# Disconnect from the IMAP server.
set success [CkImap_Disconnect $imap]

delete_CkImap $imap
delete_CkMessageSet $messageSet