Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loImapSrc
LOCAL loImapDest
LOCAL loSbMime
LOCAL lnSeqNum

lnSuccess = 0

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

loImapSrc = CreateObject('Chilkat.Imap')

* Connect to our source IMAP server.
lnSuccess = loImapSrc.Connect("imap.example.com")
IF (lnSuccess = 0) THEN
    ? loImapSrc.LastErrorText
    RELEASE loImapSrc
    CANCEL
ENDIF

* Login to the source IMAP server
lnSuccess = loImapSrc.Login("admin@chilkatsoft.com","myPassword")
IF (lnSuccess = 0) THEN
    ? loImapSrc.LastErrorText
    RELEASE loImapSrc
    CANCEL
ENDIF

loImapDest = CreateObject('Chilkat.Imap')

* Connect to our destination IMAP server.
lnSuccess = loImapDest.Connect("mail.example-code.com")
IF (lnSuccess = 0) THEN
    ? loImapDest.LastErrorText
    RELEASE loImapSrc
    RELEASE loImapDest
    CANCEL
ENDIF

* Login to the destination IMAP server
lnSuccess = loImapDest.Login("myLogin","myPassword")
IF (lnSuccess = 0) THEN
    ? loImapDest.LastErrorText
    RELEASE loImapSrc
    RELEASE loImapDest
    CANCEL
ENDIF

* Select an IMAP mailbox on the source IMAP server
lnSuccess = loImapSrc.SelectMailbox("Inbox")
IF (lnSuccess = 0) THEN
    ? loImapSrc.LastErrorText
    RELEASE loImapSrc
    RELEASE loImapDest
    CANCEL
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('Chilkat.StringBuilder')

FOR lnSeqNum = 1 TO 10
    loSbMime.Clear()
    lnSuccess = loImapSrc.FetchSingleAsMimeSb(lnSeqNum,0,loSbMime)
    IF (lnSuccess = 0) THEN
        ? loImapSrc.LastErrorText
        RELEASE loImapSrc
        RELEASE loImapDest
        RELEASE loSbMime
        CANCEL
    ENDIF

    lnSuccess = loImapDest.AppendMimeWithFlagsSb("Inbox",loSbMime,0,0,0,0)
    IF (lnSuccess = 0) THEN
        ? loImapDest.LastErrorText
        RELEASE loImapSrc
        RELEASE loImapDest
        RELEASE loSbMime
        CANCEL
    ENDIF

NEXT

* Disconnect from the IMAP servers.
lnSuccess = loImapSrc.Disconnect()
lnSuccess = loImapDest.Disconnect()

RELEASE loImapSrc
RELEASE loImapDest
RELEASE loSbMime