Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.0.0+

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImapSrc
    Handle hoImapDest
    Variant vSbMime
    Handle hoSbMime
    Integer iSeqNum
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

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

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

    //  Connect to our source IMAP server.
    Get ComConnect Of hoImapSrc "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImapSrc To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Login to the source IMAP server
    Get ComLogin Of hoImapSrc "admin@chilkatsoft.com" "myPassword" To iSuccess
    If (iSuccess = False) 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.
    Get ComConnect Of hoImapDest "mail.example-code.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImapDest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Login to the destination IMAP server
    Get ComLogin Of hoImapDest "myLogin" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImapDest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Select an IMAP mailbox on the source IMAP server
    Get ComSelectMailbox Of hoImapSrc "Inbox" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImapSrc To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  After selecting a mailbox, the NumMessages property will
    //  be updated to reflect the total number of emails in the mailbox:
    Get ComNumMessages Of hoImapSrc To iTemp1
    Showln iTemp1

    //  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
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbMime
    If (Not(IsComObjectCreated(hoSbMime))) Begin
        Send CreateComObject of hoSbMime
    End

    For iSeqNum From 1 To 10
        Send ComClear To hoSbMime
        Get pvComObject of hoSbMime to vSbMime
        Get ComFetchSingleAsMimeSb Of hoImapSrc iSeqNum False vSbMime To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoImapSrc To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get pvComObject of hoSbMime to vSbMime
        Get ComAppendMimeWithFlagsSb Of hoImapDest "Inbox" vSbMime False False False False To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoImapDest To sTemp1
            Showln sTemp1
            Procedure_Return
        End

    Loop

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


End_Procedure