Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

// 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.
loImap = createobject("CkImap")

// Turn on session logging:
loImap.KeepSessionLog = .T.

// Connect to an IMAP server.
// Use TLS
loImap.Ssl = .T.
loImap.Port = 993
llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Login
llSuccess = loImap.Login("***","***")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Select an IMAP mailbox
llSuccess = loImap.SelectMailbox("Inbox.testing.a")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llFetchUids = .T.

// Get the message IDs for all emails having "Re:" in the subject.
loMessageSet = createobject("CkMessageSet")
llSuccess = loImap.QueryMbx("SUBJECT Re:",llFetchUids,loMessageSet)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMessageSet
    return
endif

// Copy the messages from "Inbox.testing.a" to "Inbox.testing.b" in one call to CopyMultiple:
llSuccess = loImap.CopyMultiple(loMessageSet,"Inbox.testing.b")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMessageSet
    return
endif

// Alternatively, loop over each message in the set and
// copy each separately:
i = 0
do while i < loMessageSet.Count

    lnMsgId = loMessageSet.GetId(i)
    llIsUid = loMessageSet.HasUids

    llSuccess = loImap.Copy(lnMsgId,llIsUid,"Inbox.testing.c")
    if (llSuccess = .F.) then
        ? loImap.LastErrorText
        release loImap
        release loMessageSet
        return
    endif

    i = i + 1
enddo

// Display the session log.
? loImap.SessionLog

// Disconnect from the IMAP server.
llSuccess = loImap.Disconnect()


release loImap
release loMessageSet