Sample code for 30+ languages & platforms
Tcl Requires Chilkat v11.0.0+

IMAP Download All Email One at a Time

Demonstrates how to download every email in an IMAP mailbox one at a time as a MIME string or as an email object. (The MIME contains the full contents of the email including all attachments.)

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set imap [new_CkImap]

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

#  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 "myLogin" "myPassword"]
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
}

#  Once the mailbox is selected, the NumMessages property
#  will contain the number of messages in the mailbox.
#  You may loop from 1 to NumMessages to
#  fetch each message by sequence number.

set bUid 0

set n [CkImap_get_NumMessages $imap]
for {set i 1} {$i <= $n} {incr i} {

    #  Download the email by sequence number.
    set mimeStr [CkImap_fetchSingleAsMime $imap $i $bUid]

    #  ... your application may process each MIME string...
}

#  An alternative is to download each email in the form of an
#  email object, like this:
set email [new_CkEmail]

for {set i 1} {$i <= $n} {incr i} {

    #  Download the email by sequence number.
    set success [CkImap_FetchEmail $imap 0 $i $bUid $email]

    #  ... your application may process the email object...

}

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

delete_CkImap $imap
delete_CkEmail $email