Sample code for 30+ languages & platforms
Lianja

How to Copy IMAP Mail to another IMAP Server

Demonstrates how to copy the entire contents of an IMAP mailbox to another IMAP server.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

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

loImapSrc = createobject("CkImap")

// Connect to our source IMAP server.
llSuccess = loImapSrc.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImapSrc.LastErrorText
    release loImapSrc
    return
endif

// Login to the source IMAP server
llSuccess = loImapSrc.Login("admin@chilkatsoft.com","myPassword")
if (llSuccess = .F.) then
    ? loImapSrc.LastErrorText
    release loImapSrc
    return
endif

loImapDest = createobject("CkImap")

// Connect to our destination IMAP server.
llSuccess = loImapDest.Connect("mail.example-code.com")
if (llSuccess = .F.) then
    ? loImapDest.LastErrorText
    release loImapSrc
    release loImapDest
    return
endif

// Login to the destination IMAP server
llSuccess = loImapDest.Login("myLogin","myPassword")
if (llSuccess = .F.) then
    ? loImapDest.LastErrorText
    release loImapSrc
    release loImapDest
    return
endif

// Select an IMAP mailbox on the source IMAP server
llSuccess = loImapSrc.SelectMailbox("Inbox")
if (llSuccess = .F.) then
    ? loImapSrc.LastErrorText
    release loImapSrc
    release loImapDest
    return
endif

// After selecting a mailbox, the NumMessages property will
// be updated to reflect the total number of emails in the mailbox:
? str(loImapSrc.NumMessages)

// The emails in the mailbox will always have sequence numbers
// ranging from 1 to NumMessages.

// This example will copy the first 10 messages using sequence numbers
loSbMime = createobject("CkStringBuilder")

for seqNum = 1 to 10
    loSbMime.Clear()
    llSuccess = loImapSrc.FetchSingleAsMimeSb(lnSeqNum,.F.,loSbMime)
    if (llSuccess = .F.) then
        ? loImapSrc.LastErrorText
        release loImapSrc
        release loImapDest
        release loSbMime
        return
    endif

    llSuccess = loImapDest.AppendMimeWithFlagsSb("Inbox",loSbMime,.F.,.F.,.F.,.F.)
    if (llSuccess = .F.) then
        ? loImapDest.LastErrorText
        release loImapSrc
        release loImapDest
        release loSbMime
        return
    endif

next

// Disconnect from the IMAP servers.
llSuccess = loImapSrc.Disconnect()
llSuccess = loImapDest.Disconnect()


release loImapSrc
release loImapDest
release loSbMime