Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImapSrc
    Handle hoImapDest
    Boolean iFetchUids
    Variant vMset
    Handle hoMset
    Handle hoFac
    Handle hoMsetAlreadyCopied
    String sStrMsgSet
    Integer iNumUids
    Handle hoSbFlags
    Integer i
    Integer iUid
    String sFlags
    String sMimeStr
    Boolean iSeen
    Boolean iFlagged
    Boolean iAnswered
    Boolean iDraft
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatImap)) To hoImapSrc
    If (Not(IsComObjectCreated(hoImapSrc))) Begin
        Send CreateComObject of hoImapSrc
    End

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

    // Connect to our source IMAP server.
    Set ComSsl Of hoImapSrc To True
    Set ComPort Of hoImapSrc To 993
    Get ComConnect Of hoImapSrc "MY-IMAP-DOMAIN" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoImapSrc To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Login to the source IMAP server
    Get ComLogin Of hoImapSrc "MY-IMAP-LOGIN" "MY-IMAP-PASSWORD" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoImapSrc To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatImap)) To hoImapDest
    If (Not(IsComObjectCreated(hoImapDest))) Begin
        Send CreateComObject of hoImapDest
    End

    // Connect to our destination IMAP server.
    Set ComSsl Of hoImapDest To True
    Set ComPort Of hoImapDest To 993
    Get ComConnect Of hoImapDest "MY-IMAP-DOMAIN2" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoImapDest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Login to the destination IMAP server
    Get ComLogin Of hoImapDest "MY-IMAP-LOGIN2" "MY-IMAP-PASSWORD2" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoImapDest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Select a source IMAP mailbox on the source IMAP server
    Get ComSelectMailbox Of hoImapSrc "Inbox" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoImapSrc To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move True To iFetchUids

    // Get the set of UIDs for all emails on the source server.
    Get ComSearch Of hoImapSrc "ALL" iFetchUids To vMset
    If (IsComObject(vMset)) Begin
        Get Create (RefClass(cComChilkatMessageSet)) To hoMset
        Set pvComObject Of hoMset To vMset
    End
    Get ComLastMethodSuccess Of hoImapSrc To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoImapSrc To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Load the complete set of UIDs that were previously copied.
    // We dont' want to copy any of these to the destination.
    Get Create (RefClass(cComCkFileAccess)) To hoFac
    If (Not(IsComObjectCreated(hoFac))) Begin
        Send CreateComObject of hoFac
    End
    Get Create (RefClass(cComChilkatMessageSet)) To hoMsetAlreadyCopied
    If (Not(IsComObjectCreated(hoMsetAlreadyCopied))) Begin
        Send CreateComObject of hoMsetAlreadyCopied
    End
    Get ComReadEntireTextFile Of hoFac "qa_cache/saAlreadyLoaded.txt" "utf-8" To sStrMsgSet
    Get ComLastMethodSuccess Of hoFac To bTemp1
    If (bTemp1 = True) Begin
        Get ComFromCompactString Of hoMsetAlreadyCopied sStrMsgSet To iSuccess
    End

    Get ComCount Of hoMset To iNumUids
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbFlags
    If (Not(IsComObjectCreated(hoSbFlags))) Begin
        Send CreateComObject of hoSbFlags
    End

    Move 0 To i
    While (i < iNumUids)

        // If this UID was not already copied...
        Get ComGetId Of hoMset i To iUid
        Get ComContainsId Of hoMsetAlreadyCopied iUid To bTemp1
        If (Not bTemp1) Begin

            Showln "copying " iUid "..."

            // Get the flags.
            Get ComFetchFlags Of hoImapSrc iUid True To sFlags
            Get ComLastMethodSuccess Of hoImapSrc To bTemp1
            If (bTemp1 = False) Begin
                Get ComLastErrorText Of hoImapSrc To sTemp1
                Showln sTemp1
                Procedure_Return
            End

            Get ComSetString Of hoSbFlags sFlags To iSuccess

            // Get the MIME of this email from the source.
            Get ComFetchSingleAsMime Of hoImapSrc iUid True To sMimeStr
            Get ComLastMethodSuccess Of hoImapSrc To bTemp1
            If (bTemp1 = False) Begin
                Get ComLastErrorText Of hoImapSrc To sTemp1
                Showln sTemp1
                Procedure_Return
            End

            Get ComContains Of hoSbFlags "\Seen" False To iSeen
            Get ComContains Of hoSbFlags "\Flagged" False To iFlagged
            Get ComContains Of hoSbFlags "\Answered" False To iAnswered
            Get ComContains Of hoSbFlags "\Draft" False To iDraft

            Get ComAppendMimeWithFlags Of hoImapDest "Inbox" sMimeStr iSeen iFlagged iAnswered iDraft To iSuccess
            If (iSuccess <> True) Begin
                Get ComLastErrorText Of hoImapDest To sTemp1
                Showln sTemp1
                Procedure_Return
            End

            // Update msetAlreadyCopied with the uid just copied.
            Send ComInsertId To hoMsetAlreadyCopied iUid

            // Save at every iteration just in case there's a failure..
            Get ComToCompactString Of hoMsetAlreadyCopied To sStrMsgSet
            Get ComWriteEntireTextFile Of hoFac "qa_cache/saAlreadyLoaded.txt" sStrMsgSet "utf-8" False To iSuccess
        End

        Move (i + 1) To i
    Loop

    Send Destroy of hoMset

    // Disconnect from the IMAP servers.
    Get ComDisconnect Of hoImapSrc To iSuccess
    Get ComDisconnect Of hoImapDest To iSuccess


End_Procedure