Sample code for 30+ languages & platforms
Tcl

Fetch a Single IMAP Message into an Email Object

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchEmail method, which downloads one message (or its headers) into an Email object. The arguments are headerOnly, the message id, bUid (UID vs sequence number), and the Email that receives the message. This example fetches message 1 in full and reads its subject and from.

Background: FetchEmail parses a downloaded message into a fully navigable Email object, giving you its subject, sender, body, and attachments. Passing headerOnly = true downloads just the headers — a fast way to build a message-list preview — after which you can fetch the full message on demand. Because a mailbox must be selected first, and fetching a full message can set the \Seen flag, use ExamineMailbox instead of SelectMailbox when you must not change read state.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the Imap.FetchEmail method, which downloads one message (or its headers) into
#  an Email object.  The 1st argument is headerOnly, the 2nd is the message id, the 3rd
#  (bUid) selects UID vs sequence number, and the 4th is the Email that receives the message.

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
}

#  Download message sequence number 1 in full.  headerOnly=0, bUid=0.
set email [new_CkEmail]

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

puts "Subject: [CkEmail_subject $email]"
puts "From: [CkEmail_from $email]"

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


delete_CkImap $imap
delete_CkEmail $email