Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oEmail

nSuccess := 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.

oImap := CreateObject("Chilkat.Imap")

oImap:Ssl := 1
oImap:Port := 993

nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:Login("user@example.com", "myPassword")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Build the email to be uploaded.
oEmail := CreateObject("Chilkat.Email")
oEmail:Subject := "Meeting agenda"
oEmail:From := "Alice <alice@example.com>"
nSuccess := oEmail:AddTo("Bob", "bob@example.com")
oEmail:Body := "Let's meet at 10am to review the agenda."

//  Append (upload) the email to the "Drafts" mailbox.
nSuccess := oImap:AppendMail("Drafts", oEmail)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oEmail:destroy()
    RETURN
ENDIF

//  AppendUid holds the UID assigned by the server (0 if the server did not report one).
? "Appended UID: " + Str(oImap:AppendUid)

nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oEmail:destroy()
    RETURN
ENDIF

oImap:destroy()
oEmail:destroy()