Sample code for 30+ languages & platforms
DataFlex

Copy an Email from One Mailbox to Another

Copies an email from one IMAP folder to another. After running this example, copies of the email will be present in both source and destination folders.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Boolean iFetchUids
    Variant vMessageSet
    Handle hoMessageSet
    Integer i
    Integer iMsgId
    Boolean iIsUid
    String sTemp1

    Move False To iSuccess

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

    // This example copies an email from one mailbox to another.
    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End

    // Turn on session logging:
    Set ComKeepSessionLog Of hoImap To True

    // Connect to an IMAP server.
    // Use TLS
    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993
    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Login
    Get ComLogin Of hoImap "***" "***" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Select an IMAP mailbox
    Get ComSelectMailbox Of hoImap "Inbox.testing.a" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move True To iFetchUids

    // Get the message IDs for all emails having "Re:" in the subject.
    Get Create (RefClass(cComChilkatMessageSet)) To hoMessageSet
    If (Not(IsComObjectCreated(hoMessageSet))) Begin
        Send CreateComObject of hoMessageSet
    End
    Get pvComObject of hoMessageSet to vMessageSet
    Get ComQueryMbx Of hoImap "SUBJECT Re:" iFetchUids vMessageSet To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Copy the messages from "Inbox.testing.a" to "Inbox.testing.b" in one call to CopyMultiple:
    Get pvComObject of hoMessageSet to vMessageSet
    Get ComCopyMultiple Of hoImap vMessageSet "Inbox.testing.b" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Alternatively, loop over each message in the set and
    // copy each separately:
    Move 0 To i
    While (i < (ComCount(hoMessageSet)))

        Get ComGetId Of hoMessageSet i To iMsgId
        Get ComHasUids Of hoMessageSet To iIsUid

        Get ComCopy Of hoImap iMsgId iIsUid "Inbox.testing.c" To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoImap To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Move (i + 1) To i
    Loop

    // Display the session log.
    Get ComSessionLog Of hoImap To sTemp1
    Showln sTemp1

    // Disconnect from the IMAP server.
    Get ComDisconnect Of hoImap To iSuccess


End_Procedure