Sample code for 30+ languages & platforms
Tcl

Imap.GetMailSize vs Email.Size

Shows how to get the total size of an email, as well as the sizes of the attachments. This can be done when either full-emails or headers-only are downloaded.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This example assumes 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
}

# Get the message IDs of all the emails in the mailbox
# We can choose to fetch UIDs or sequence numbers.
set fetchUids 1
set messageSet [new_CkMessageSet]

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

# When downloading headers, each email object contains
# (obviously) the headers, but the body will be missing.
# Attachments will not be included.  However, it is
# possible to get information about the attachments
# as well as the complete size of the email.
set bundle [new_CkEmailBundle]

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

# Loop over the email objects and display information about each:
set email [new_CkEmail]

set i 0
set j 0
set numEmails [CkEmailBundle_get_MessageCount $bundle]
while {$i < $numEmails} {
    CkEmailBundle_EmailAt $bundle $i $email

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

    # Display the recipients:
    set j 0
    while {$j < [CkEmail_get_NumTo $email]} {
        puts "TO: [CkEmail_getToName $email $j], [CkEmail_getToAddr $email $j]"
        set j [expr $j + 1]
    }
    set j 0
    while {$j < [CkEmail_get_NumCC $email]} {
        puts "CC: [CkEmail_getCcName $email $j], [CkEmail_getCcAddr $email $j]"
        set j [expr $j + 1]
    }

    # 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]
    set j 0
    while {$j < $numAttach} {
        puts [CkImap_getMailAttachFilename $imap $email $j]
        set attachSize [CkImap_GetMailAttachSize $imap $email $j]
        puts "    size = $attachSize bytes"
        set j [expr $j + 1]
    }

    puts "--"

    set i [expr $i + 1]
}

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

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