Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkImapW.h>
#include <C_CkEmailW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;
    HCkEmailW email;

    success = FALSE;

    //  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.

    imap = CkImapW_Create();

    CkImapW_putSsl(imap,TRUE);
    CkImapW_putPort(imap,993);

    success = CkImapW_Connect(imap,L"imap.example.com");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    success = CkImapW_Login(imap,L"user@example.com",L"myPassword");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    //  Build the email to be uploaded.
    email = CkEmailW_Create();
    CkEmailW_putSubject(email,L"Meeting agenda");
    CkEmailW_putFrom(email,L"Alice <alice@example.com>");
    success = CkEmailW_AddTo(email,L"Bob",L"bob@example.com");
    CkEmailW_putBody(email,L"Let's meet at 10am to review the agenda.");

    //  Append (upload) the email to the "Drafts" mailbox.
    success = CkImapW_AppendMail(imap,L"Drafts",email);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkEmailW_Dispose(email);
        return;
    }

    //  AppendUid holds the UID assigned by the server (0 if the server did not report one).
    wprintf(L"Appended UID: %d\n",CkImapW_getAppendUid(imap));

    success = CkImapW_Disconnect(imap);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkEmailW_Dispose(email);
        return;
    }



    CkImapW_Dispose(imap);
    CkEmailW_Dispose(email);

    }