Sample code for 30+ languages & platforms
Unicode C

How to Copy IMAP Mail to another IMAP Server

Demonstrates how to copy the entire contents of an IMAP mailbox to another IMAP server.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imapSrc;
    HCkImapW imapDest;
    HCkStringBuilderW sbMime;
    int seqNum;

    success = FALSE;

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

    imapSrc = CkImapW_Create();

    // Connect to our source IMAP server.
    success = CkImapW_Connect(imapSrc,L"imap.example.com");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imapSrc));
        CkImapW_Dispose(imapSrc);
        return;
    }

    // Login to the source IMAP server
    success = CkImapW_Login(imapSrc,L"admin@chilkatsoft.com",L"myPassword");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imapSrc));
        CkImapW_Dispose(imapSrc);
        return;
    }

    imapDest = CkImapW_Create();

    // Connect to our destination IMAP server.
    success = CkImapW_Connect(imapDest,L"mail.example-code.com");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imapDest));
        CkImapW_Dispose(imapSrc);
        CkImapW_Dispose(imapDest);
        return;
    }

    // Login to the destination IMAP server
    success = CkImapW_Login(imapDest,L"myLogin",L"myPassword");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imapDest));
        CkImapW_Dispose(imapSrc);
        CkImapW_Dispose(imapDest);
        return;
    }

    // Select an IMAP mailbox on the source IMAP server
    success = CkImapW_SelectMailbox(imapSrc,L"Inbox");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imapSrc));
        CkImapW_Dispose(imapSrc);
        CkImapW_Dispose(imapDest);
        return;
    }

    // After selecting a mailbox, the NumMessages property will
    // be updated to reflect the total number of emails in the mailbox:
    wprintf(L"%d\n",CkImapW_getNumMessages(imapSrc));

    // The emails in the mailbox will always have sequence numbers
    // ranging from 1 to NumMessages.

    // This example will copy the first 10 messages using sequence numbers
    sbMime = CkStringBuilderW_Create();

    for (seqNum = 1; seqNum <= 10; seqNum++) {
        CkStringBuilderW_Clear(sbMime);
        success = CkImapW_FetchSingleAsMimeSb(imapSrc,seqNum,FALSE,sbMime);
        if (success == FALSE) {
            wprintf(L"%s\n",CkImapW_lastErrorText(imapSrc));
            CkImapW_Dispose(imapSrc);
            CkImapW_Dispose(imapDest);
            CkStringBuilderW_Dispose(sbMime);
            return;
        }

        success = CkImapW_AppendMimeWithFlagsSb(imapDest,L"Inbox",sbMime,FALSE,FALSE,FALSE,FALSE);
        if (success == FALSE) {
            wprintf(L"%s\n",CkImapW_lastErrorText(imapDest));
            CkImapW_Dispose(imapSrc);
            CkImapW_Dispose(imapDest);
            CkStringBuilderW_Dispose(sbMime);
            return;
        }

    }

    // Disconnect from the IMAP servers.
    success = CkImapW_Disconnect(imapSrc);
    success = CkImapW_Disconnect(imapDest);


    CkImapW_Dispose(imapSrc);
    CkImapW_Dispose(imapDest);
    CkStringBuilderW_Dispose(sbMime);

    }