Xbase++
Xbase++
Copy Email from one IMAP Account to Another
See more IMAP Examples
Demonstrates how to copy the email in a mailbox from one account to another.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oImapSrc
LOCAL oImapDest
LOCAL nFetchUids
LOCAL oMset
LOCAL oFac
LOCAL oMsetAlreadyCopied
LOCAL cStrMsgSet
LOCAL nNumUids
LOCAL oSbFlags
LOCAL i
LOCAL nUid
LOCAL cFlags
LOCAL cMimeStr
LOCAL nSeen
LOCAL nFlagged
LOCAL nAnswered
LOCAL nDraft
nSuccess := 0
oImapSrc := CreateObject("Chilkat.Imap")
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Connect to our source IMAP server.
oImapSrc:Ssl := 1
oImapSrc:Port := 993
nSuccess := oImapSrc:Connect("MY-IMAP-DOMAIN")
IF (nSuccess != 1)
? oImapSrc:LastErrorText
oImapSrc:destroy()
RETURN
ENDIF
// Login to the source IMAP server
nSuccess := oImapSrc:Login("MY-IMAP-LOGIN", "MY-IMAP-PASSWORD")
IF (nSuccess != 1)
? oImapSrc:LastErrorText
oImapSrc:destroy()
RETURN
ENDIF
oImapDest := CreateObject("Chilkat.Imap")
// Connect to our destination IMAP server.
oImapDest:Ssl := 1
oImapDest:Port := 993
nSuccess := oImapDest:Connect("MY-IMAP-DOMAIN2")
IF (nSuccess != 1)
? oImapDest:LastErrorText
oImapSrc:destroy()
oImapDest:destroy()
RETURN
ENDIF
// Login to the destination IMAP server
nSuccess := oImapDest:Login("MY-IMAP-LOGIN2", "MY-IMAP-PASSWORD2")
IF (nSuccess != 1)
? oImapDest:LastErrorText
oImapSrc:destroy()
oImapDest:destroy()
RETURN
ENDIF
// Select a source IMAP mailbox on the source IMAP server
nSuccess := oImapSrc:SelectMailbox("Inbox")
IF (nSuccess != 1)
? oImapSrc:LastErrorText
oImapSrc:destroy()
oImapDest:destroy()
RETURN
ENDIF
nFetchUids := 1
// Get the set of UIDs for all emails on the source server.
oMset := oImapSrc:Search("ALL", nFetchUids)
IF (oImapSrc:LastMethodSuccess != 1)
? oImapSrc:LastErrorText
oImapSrc:destroy()
oImapDest:destroy()
RETURN
ENDIF
// Load the complete set of UIDs that were previously copied.
// We dont' want to copy any of these to the destination.
oFac := CreateObject("Chilkat.FileAccess")
oMsetAlreadyCopied := CreateObject("Chilkat.MessageSet")
cStrMsgSet := oFac:ReadEntireTextFile("qa_cache/saAlreadyLoaded.txt", "utf-8")
IF (oFac:LastMethodSuccess == 1)
oMsetAlreadyCopied:FromCompactString(cStrMsgSet)
ENDIF
nNumUids := oMset:Count
oSbFlags := CreateObject("Chilkat.StringBuilder")
i := 0
DO WHILE i < nNumUids
// If this UID was not already copied...
nUid := oMset:GetId(i)
IF (.NOT. oMsetAlreadyCopied:ContainsId(nUid))
? "copying " + Str(nUid) + "..."
// Get the flags.
cFlags := oImapSrc:FetchFlags(nUid, 1)
IF (oImapSrc:LastMethodSuccess == 0)
? oImapSrc:LastErrorText
oImapSrc:destroy()
oImapDest:destroy()
oFac:destroy()
oMsetAlreadyCopied:destroy()
oSbFlags:destroy()
RETURN
ENDIF
oSbFlags:SetString(cFlags)
// Get the MIME of this email from the source.
cMimeStr := oImapSrc:FetchSingleAsMime(nUid, 1)
IF (oImapSrc:LastMethodSuccess == 0)
? oImapSrc:LastErrorText
oImapSrc:destroy()
oImapDest:destroy()
oFac:destroy()
oMsetAlreadyCopied:destroy()
oSbFlags:destroy()
RETURN
ENDIF
nSeen := oSbFlags:Contains("\Seen", 0)
nFlagged := oSbFlags:Contains("\Flagged", 0)
nAnswered := oSbFlags:Contains("\Answered", 0)
nDraft := oSbFlags:Contains("\Draft", 0)
nSuccess := oImapDest:AppendMimeWithFlags("Inbox", cMimeStr, nSeen, nFlagged, nAnswered, nDraft)
IF (nSuccess != 1)
? oImapDest:LastErrorText
oImapSrc:destroy()
oImapDest:destroy()
oFac:destroy()
oMsetAlreadyCopied:destroy()
oSbFlags:destroy()
RETURN
ENDIF
// Update msetAlreadyCopied with the uid just copied.
oMsetAlreadyCopied:InsertId(nUid)
// Save at every iteration just in case there's a failure..
cStrMsgSet := oMsetAlreadyCopied:ToCompactString()
oFac:WriteEntireTextFile("qa_cache/saAlreadyLoaded.txt", cStrMsgSet, "utf-8", 0)
ENDIF
i := i + 1
ENDDO
oMset:destroy()
// Disconnect from the IMAP servers.
nSuccess := oImapSrc:Disconnect()
nSuccess := oImapDest:Disconnect()
oImapSrc:destroy()
oImapDest:destroy()
oFac:destroy()
oMsetAlreadyCopied:destroy()
oSbFlags:destroy()