Sample code for 30+ languages & platforms
Unicode C

Copy Email from one IMAP Account to Another

See more IMAP Examples

Demonstrates how to copy the email in a mailbox from one account to another.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imapSrc;
    HCkImapW imapDest;
    BOOL fetchUids;
    HCkMessageSetW mset;
    HCkFileAccessW fac;
    HCkMessageSetW msetAlreadyCopied;
    const wchar_t *strMsgSet;
    int numUids;
    HCkStringBuilderW sbFlags;
    int i;
    int uid;
    const wchar_t *flags;
    const wchar_t *mimeStr;
    BOOL seen;
    BOOL flagged;
    BOOL answered;
    BOOL draft;

    success = FALSE;

    imapSrc = CkImapW_Create();

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

    // Connect to our source IMAP server.
    CkImapW_putSsl(imapSrc,TRUE);
    CkImapW_putPort(imapSrc,993);
    success = CkImapW_Connect(imapSrc,L"MY-IMAP-DOMAIN");
    if (success != TRUE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imapSrc));
        CkImapW_Dispose(imapSrc);
        return;
    }

    // Login to the source IMAP server
    success = CkImapW_Login(imapSrc,L"MY-IMAP-LOGIN",L"MY-IMAP-PASSWORD");
    if (success != TRUE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imapSrc));
        CkImapW_Dispose(imapSrc);
        return;
    }

    imapDest = CkImapW_Create();

    // Connect to our destination IMAP server.
    CkImapW_putSsl(imapDest,TRUE);
    CkImapW_putPort(imapDest,993);
    success = CkImapW_Connect(imapDest,L"MY-IMAP-DOMAIN2");
    if (success != TRUE) {
        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"MY-IMAP-LOGIN2",L"MY-IMAP-PASSWORD2");
    if (success != TRUE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imapDest));
        CkImapW_Dispose(imapSrc);
        CkImapW_Dispose(imapDest);
        return;
    }

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

    fetchUids = TRUE;

    // Get the set of UIDs for all emails on the source server.
    mset = CkImapW_Search(imapSrc,L"ALL",fetchUids);
    if (CkImapW_getLastMethodSuccess(imapSrc) != TRUE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imapSrc));
        CkImapW_Dispose(imapSrc);
        CkImapW_Dispose(imapDest);
        return;
    }

    // Load the complete set of UIDs that were previously copied.
    // We dont' want to copy any of these to the destination.
    fac = CkFileAccessW_Create();
    msetAlreadyCopied = CkMessageSetW_Create();
    strMsgSet = CkFileAccessW_readEntireTextFile(fac,L"qa_cache/saAlreadyLoaded.txt",L"utf-8");
    if (CkFileAccessW_getLastMethodSuccess(fac) == TRUE) {
        CkMessageSetW_FromCompactString(msetAlreadyCopied,strMsgSet);
    }

    numUids = CkMessageSetW_getCount(mset);
    sbFlags = CkStringBuilderW_Create();

    i = 0;
    while (i < numUids) {

        // If this UID was not already copied...
        uid = CkMessageSetW_GetId(mset,i);
        if (!CkMessageSetW_ContainsId(msetAlreadyCopied,uid)) {

            wprintf(L"copying %d...\n",uid);

            // Get the flags.
            flags = CkImapW_fetchFlags(imapSrc,uid,TRUE);
            if (CkImapW_getLastMethodSuccess(imapSrc) == FALSE) {
                wprintf(L"%s\n",CkImapW_lastErrorText(imapSrc));
                CkImapW_Dispose(imapSrc);
                CkImapW_Dispose(imapDest);
                CkFileAccessW_Dispose(fac);
                CkMessageSetW_Dispose(msetAlreadyCopied);
                CkStringBuilderW_Dispose(sbFlags);
                return;
            }

            CkStringBuilderW_SetString(sbFlags,flags);

            // Get the MIME of this email from the source.
            mimeStr = CkImapW_fetchSingleAsMime(imapSrc,uid,TRUE);
            if (CkImapW_getLastMethodSuccess(imapSrc) == FALSE) {
                wprintf(L"%s\n",CkImapW_lastErrorText(imapSrc));
                CkImapW_Dispose(imapSrc);
                CkImapW_Dispose(imapDest);
                CkFileAccessW_Dispose(fac);
                CkMessageSetW_Dispose(msetAlreadyCopied);
                CkStringBuilderW_Dispose(sbFlags);
                return;
            }

            seen = CkStringBuilderW_Contains(sbFlags,L"\\Seen",FALSE);
            flagged = CkStringBuilderW_Contains(sbFlags,L"\\Flagged",FALSE);
            answered = CkStringBuilderW_Contains(sbFlags,L"\\Answered",FALSE);
            draft = CkStringBuilderW_Contains(sbFlags,L"\\Draft",FALSE);

            success = CkImapW_AppendMimeWithFlags(imapDest,L"Inbox",mimeStr,seen,flagged,answered,draft);
            if (success != TRUE) {
                wprintf(L"%s\n",CkImapW_lastErrorText(imapDest));
                CkImapW_Dispose(imapSrc);
                CkImapW_Dispose(imapDest);
                CkFileAccessW_Dispose(fac);
                CkMessageSetW_Dispose(msetAlreadyCopied);
                CkStringBuilderW_Dispose(sbFlags);
                return;
            }

            // Update msetAlreadyCopied with the uid just copied.
            CkMessageSetW_InsertId(msetAlreadyCopied,uid);

            // Save at every iteration just in case there's a failure..
            strMsgSet = CkMessageSetW_toCompactString(msetAlreadyCopied);
            CkFileAccessW_WriteEntireTextFile(fac,L"qa_cache/saAlreadyLoaded.txt",strMsgSet,L"utf-8",FALSE);
        }

        i = i + 1;
    }

    CkMessageSetW_Dispose(mset);

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


    CkImapW_Dispose(imapSrc);
    CkImapW_Dispose(imapDest);
    CkFileAccessW_Dispose(fac);
    CkMessageSetW_Dispose(msetAlreadyCopied);
    CkStringBuilderW_Dispose(sbFlags);

    }