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

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nFetchUids
LOCAL oMessageSet
LOCAL i
LOCAL nMsgId
LOCAL nIsUid

nSuccess := 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.
oImap := CreateObject("Chilkat.Imap")

//  Turn on session logging:
oImap:KeepSessionLog := 1

//  Connect to an IMAP server.
//  Use TLS
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Login
nSuccess := oImap:Login("***", "***")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox.testing.a")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nFetchUids := 1

//  Get the message IDs for all emails having "Re:" in the subject.
oMessageSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("SUBJECT Re:", nFetchUids, oMessageSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

//  Copy the messages from "Inbox.testing.a" to "Inbox.testing.b" in one call to CopyMultiple:
nSuccess := oImap:CopyMultiple(oMessageSet, "Inbox.testing.b")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

//  Alternatively, loop over each message in the set and
//  copy each separately:
i := 0
DO WHILE i < oMessageSet:Count

    nMsgId := oMessageSet:GetId(i)
    nIsUid := oMessageSet:HasUids

    nSuccess := oImap:Copy(nMsgId, nIsUid, "Inbox.testing.c")
    IF (nSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oMessageSet:destroy()
        RETURN
    ENDIF

    i := i + 1
ENDDO

//  Display the session log.
? oImap:SessionLog

//  Disconnect from the IMAP server.
nSuccess := oImap:Disconnect()

oImap:destroy()
oMessageSet:destroy()