Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkImap.pb"
IncludeFile "CkMessageSet.pb"

Procedure ChilkatExample()

    success.i = 0

    ; 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.
    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Turn on session logging:
    CkImap::setCkKeepSessionLog(imap, 1)

    ; Connect to an IMAP server.
    ; Use TLS
    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)
    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Login
    success = CkImap::ckLogin(imap,"***","***")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Select an IMAP mailbox
    success = CkImap::ckSelectMailbox(imap,"Inbox.testing.a")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    fetchUids.i = 1

    ; Get the message IDs for all emails having "Re:" in the subject.
    messageSet.i = CkMessageSet::ckCreate()
    If messageSet.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckQueryMbx(imap,"SUBJECT Re:",fetchUids,messageSet)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(messageSet)
        ProcedureReturn
    EndIf

    ; Copy the messages from "Inbox.testing.a" to "Inbox.testing.b" in one call to CopyMultiple:
    success = CkImap::ckCopyMultiple(imap,messageSet,"Inbox.testing.b")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(messageSet)
        ProcedureReturn
    EndIf

    ; Alternatively, loop over each message in the set and
    ; copy each separately:
    i.i = 0
    While i < CkMessageSet::ckCount(messageSet)

        msgId.i = CkMessageSet::ckGetId(messageSet,i)
        isUid.i = CkMessageSet::ckHasUids(messageSet)

        success = CkImap::ckCopy(imap,msgId,isUid,"Inbox.testing.c")
        If success = 0
            Debug CkImap::ckLastErrorText(imap)
            CkImap::ckDispose(imap)
            CkMessageSet::ckDispose(messageSet)
            ProcedureReturn
        EndIf

        i = i + 1
    Wend

    ; Display the session log.
    Debug CkImap::ckSessionLog(imap)

    ; Disconnect from the IMAP server.
    success = CkImap::ckDisconnect(imap)


    CkImap::ckDispose(imap)
    CkMessageSet::ckDispose(messageSet)


    ProcedureReturn
EndProcedure