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

Move IMAP Messages with OAuth2 and the MOVE Extension

See more IMAP Examples

Move matching messages between two existing mailboxes using QueryMbx and MoveMessages. Gmail OAuth2 supplies the concrete connection example. Other servers require their own authentication settings.

This changes all matching messages in the source folder. Choose the source, destination, and search criterion before running it. Gmail custom folders represent labels; moving between them changes label membership. The code checks MOVE at runtime, handles an empty search, and uses UID-based message sets. It does not fall back to copy/delete/EXPUNGE or print authentication logs. Confirm the result before retrying after a lost connection.

First authorize the mailbox for https://mail.google.com/ and refresh the token as needed. Read the current token from gmail-mail-token.json in a private working directory. Narrow Gmail REST scopes do not authorize these protocols. Keep the token and authentication logs private.

See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.

Reference documentation

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoToken
    String sMailbox
    Handle hoImap
    String sCapabilities
    String sSourceFolder
    String sDestinationFolder
    Variant vSelected
    Handle hoSelected
    String sTemp1
    Integer iTemp1
    Boolean bTemp1
    Boolean bTemp2

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    Get Create (RefClass(cComChilkatJsonObject)) To hoToken
    If (Not(IsComObjectCreated(hoToken))) Begin
        Send CreateComObject of hoToken
    End
    Get ComLoadFile Of hoToken "gmail-mail-token.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not read gmail-mail-token.json from the private working directory."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
    Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
    If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
        Showln "An OAuth access token is required. Authorize or refresh the account first."
        Procedure_Return
    End

    Move "user@example.com" To sMailbox
    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End
    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993
    Set ComRequireSslCertVerify Of hoImap To True
    Set ComAuthMethod Of hoImap To "XOAUTH2"
    Get ComConnect Of hoImap "imap.gmail.com" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not establish the Gmail IMAP TLS connection."
        Procedure_Return
    End

    Get ComStringOf Of hoToken "access_token" To sTemp1
    Get ComLogin Of hoImap sMailbox sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Get ComDisconnect Of hoImap To iSuccess
        Showln "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
        Procedure_Return
    End

    Get ComCapability Of hoImap To sCapabilities
    Get ComLastMethodSuccess Of hoImap To bTemp1
    Get ComHasCapability Of hoImap "MOVE" sCapabilities To bTemp2
    If ((bTemp1 <> True) Or (bTemp2 <> True)) Begin
        Get ComLogout Of hoImap To iSuccess
        Get ComDisconnect Of hoImap To iSuccess
        Showln "This example requires the MOVE capability."
        Procedure_Return
    End

    //  These existing custom-label folders are inputs. The operation moves matching messages.
    Move "Example" To sSourceFolder
    Move "Example/Processed" To sDestinationFolder
    Get ComSelectMailbox Of hoImap sSourceFolder To iSuccess
    If (iSuccess <> True) Begin
        Get ComLogout Of hoImap To iSuccess
        Get ComDisconnect Of hoImap To iSuccess
        Showln "Could not select the source folder."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatMessageSet)) To hoSelected
    If (Not(IsComObjectCreated(hoSelected))) Begin
        Send CreateComObject of hoSelected
    End
    Get pvComObject of hoSelected to vSelected
    Get ComQueryMbx Of hoImap "FROM sender@example.net" True vSelected To iSuccess
    If (iSuccess <> True) Begin
        Get ComLogout Of hoImap To iSuccess
        Get ComDisconnect Of hoImap To iSuccess
        Showln "Could not search the source folder."
        Procedure_Return
    End

    Get ComCount Of hoSelected To iTemp1
    If (iTemp1 = 0) Begin
        Get ComLogout Of hoImap To iSuccess
        Get ComDisconnect Of hoImap To iSuccess
        Showln "No matching messages; nothing was moved."
        Procedure_Return
    End

    Get ComToCompactString Of hoSelected To sTemp1
    Showln "Moving UID set: " sTemp1
    Get pvComObject of hoSelected to vSelected
    Get ComMoveMessages Of hoImap vSelected sDestinationFolder To iSuccess
    If (iSuccess <> True) Begin
        Get ComLogout Of hoImap To iSuccess
        Get ComDisconnect Of hoImap To iSuccess
        Showln "Move failed. Verify the outcome before retrying an interrupted operation."
        Procedure_Return
    End

    Showln "MOVE completed for the selected messages."
    Get ComLogout Of hoImap To iSuccess
    Get ComDisconnect Of hoImap To iSuccess


End_Procedure