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

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

Xbase++
LOCAL nSuccess
LOCAL oImapSrc
LOCAL oImapDest
LOCAL oSbMime
LOCAL nSeqNum

nSuccess := 0

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

oImapSrc := CreateObject("Chilkat.Imap")

//  Connect to our source IMAP server.
nSuccess := oImapSrc:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImapSrc:LastErrorText
    oImapSrc:destroy()
    RETURN
ENDIF

//  Login to the source IMAP server
nSuccess := oImapSrc:Login("admin@chilkatsoft.com", "myPassword")
IF (nSuccess == 0)
    ? oImapSrc:LastErrorText
    oImapSrc:destroy()
    RETURN
ENDIF

oImapDest := CreateObject("Chilkat.Imap")

//  Connect to our destination IMAP server.
nSuccess := oImapDest:Connect("mail.example-code.com")
IF (nSuccess == 0)
    ? oImapDest:LastErrorText
    oImapSrc:destroy()
    oImapDest:destroy()
    RETURN
ENDIF

//  Login to the destination IMAP server
nSuccess := oImapDest:Login("myLogin", "myPassword")
IF (nSuccess == 0)
    ? oImapDest:LastErrorText
    oImapSrc:destroy()
    oImapDest:destroy()
    RETURN
ENDIF

//  Select an IMAP mailbox on the source IMAP server
nSuccess := oImapSrc:SelectMailbox("Inbox")
IF (nSuccess == 0)
    ? oImapSrc:LastErrorText
    oImapSrc:destroy()
    oImapDest:destroy()
    RETURN
ENDIF

//  After selecting a mailbox, the NumMessages property will
//  be updated to reflect the total number of emails in the mailbox:
? Str(oImapSrc: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
oSbMime := CreateObject("Chilkat.StringBuilder")

FOR nSeqNum := 1 TO 10
    oSbMime:Clear()
    nSuccess := oImapSrc:FetchSingleAsMimeSb(nSeqNum, 0, oSbMime)
    IF (nSuccess == 0)
        ? oImapSrc:LastErrorText
        oImapSrc:destroy()
        oImapDest:destroy()
        oSbMime:destroy()
        RETURN
    ENDIF

    nSuccess := oImapDest:AppendMimeWithFlagsSb("Inbox", oSbMime, 0, 0, 0, 0)
    IF (nSuccess == 0)
        ? oImapDest:LastErrorText
        oImapSrc:destroy()
        oImapDest:destroy()
        oSbMime:destroy()
        RETURN
    ENDIF

NEXT

//  Disconnect from the IMAP servers.
nSuccess := oImapSrc:Disconnect()
nSuccess := oImapDest:Disconnect()

oImapSrc:destroy()
oImapDest:destroy()
oSbMime:destroy()