Sample code for 30+ languages & platforms
Tcl

Fetch Single Email by UID or Sequence Number

Assuming the UID is known, download a single email by UID from an IMAP mail 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
}

set email [new_CkEmail]

set uid 2014
set isUid 1

set success [CkImap_FetchEmail $imap 0 $uid $isUid $email]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkEmail $email
    exit
}

# Display the From and Subject
puts [CkEmail_fromAddress $email]
puts [CkEmail_subject $email]

# Display the Body property, which is the default body.
# If an email has an HTML body, the Body property contains
# the HTML source.  Otherwise it contains the plain-text
# body.
puts "---- EMAIL BODY ----"
puts [CkEmail_body $email]
puts "--------------------"

# Display the recipients:

for {set j 0} {$j <= [expr [CkEmail_get_NumTo $email] - 1]} {incr j} {
    puts [CkEmail_getToName $email $j], [CkEmail_getToAddr $email $j]
}
for {set j 0} {$j <= [expr [CkEmail_get_NumCC $email] - 1]} {incr j} {
    puts [CkEmail_getCcName $email $j], [CkEmail_getCcAddr $email $j]
}

# Show the total size of the email, including body and attachments:
puts [CkEmail_get_Size $email]

# When a full email is downloaded, we would use the
# email.NumAttachments property in conjunction with the
# email.GetMailAttach* methods.
# However, when an email object contains only the header,
# we need to access the attachment info differently:
set numAttach [CkImap_GetMailNumAttach $imap $email]
puts "$numAttach"

for {set j 0} {$j <= [expr $numAttach - 1]} {incr j} {
    puts [CkImap_getMailAttachFilename $imap $email $j]
    set attachSize [CkImap_GetMailAttachSize $imap $email $j]
    puts "    size = $attachSize bytes"
}

puts "--"

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

delete_CkImap $imap
delete_CkEmail $email