Sample code for 30+ languages & platforms
Tcl

Append (Upload) an Email to an IMAP Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMail method, which uploads an Email object to a mailbox on the server. The first argument is the destination mailbox name and the second is the Email. After a successful append, the AppendUid property holds the UID the server assigned (or 0 if none was reported). This example uploads a newly built message to the Drafts mailbox.

Background: The IMAP APPEND command lets a client add a message directly to any mailbox without sending it through SMTP — it is how "Save to Drafts" works, and how a client keeps its own copy in Sent. The initial \Seen state is governed by the AppendSeen property (or by the flag arguments of the AppendMimeWithFlags variants).

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the Imap.AppendMail method, which uploads an Email object to a mailbox on the
#  server.  The 1st argument is the destination mailbox name and the 2nd is the Email.

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
}

#  Build the email to be uploaded.
set email [new_CkEmail]

CkEmail_put_Subject $email "Meeting agenda"
CkEmail_put_From $email "Alice <alice@example.com>"
set success [CkEmail_AddTo $email "Bob" "bob@example.com"]
CkEmail_put_Body $email "Let's meet at 10am to review the agenda."

#  Append (upload) the email to the "Drafts" mailbox.
set success [CkImap_AppendMail $imap "Drafts" $email]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkEmail $email
    exit
}

#  AppendUid holds the UID assigned by the server (0 if the server did not report one).
puts "Appended UID: [CkImap_get_AppendUid $imap]"

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