PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkImap.pb"
Procedure ChilkatExample()
success.i = 0
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
imapSrc.i = CkImap::ckCreate()
If imapSrc.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Connect to our source IMAP server.
success = CkImap::ckConnect(imapSrc,"imap.example.com")
If success = 0
Debug CkImap::ckLastErrorText(imapSrc)
CkImap::ckDispose(imapSrc)
ProcedureReturn
EndIf
; Login to the source IMAP server
success = CkImap::ckLogin(imapSrc,"admin@chilkatsoft.com","myPassword")
If success = 0
Debug CkImap::ckLastErrorText(imapSrc)
CkImap::ckDispose(imapSrc)
ProcedureReturn
EndIf
imapDest.i = CkImap::ckCreate()
If imapDest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Connect to our destination IMAP server.
success = CkImap::ckConnect(imapDest,"mail.example-code.com")
If success = 0
Debug CkImap::ckLastErrorText(imapDest)
CkImap::ckDispose(imapSrc)
CkImap::ckDispose(imapDest)
ProcedureReturn
EndIf
; Login to the destination IMAP server
success = CkImap::ckLogin(imapDest,"myLogin","myPassword")
If success = 0
Debug CkImap::ckLastErrorText(imapDest)
CkImap::ckDispose(imapSrc)
CkImap::ckDispose(imapDest)
ProcedureReturn
EndIf
; Select an IMAP mailbox on the source IMAP server
success = CkImap::ckSelectMailbox(imapSrc,"Inbox")
If success = 0
Debug CkImap::ckLastErrorText(imapSrc)
CkImap::ckDispose(imapSrc)
CkImap::ckDispose(imapDest)
ProcedureReturn
EndIf
; After selecting a mailbox, the NumMessages property will
; be updated to reflect the total number of emails in the mailbox:
Debug Str(CkImap::ckNumMessages(imapSrc))
; 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
sbMime.i = CkStringBuilder::ckCreate()
If sbMime.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
seqNum.i
For seqNum = 1 To 10
CkStringBuilder::ckClear(sbMime)
success = CkImap::ckFetchSingleAsMimeSb(imapSrc,seqNum,0,sbMime)
If success = 0
Debug CkImap::ckLastErrorText(imapSrc)
CkImap::ckDispose(imapSrc)
CkImap::ckDispose(imapDest)
CkStringBuilder::ckDispose(sbMime)
ProcedureReturn
EndIf
success = CkImap::ckAppendMimeWithFlagsSb(imapDest,"Inbox",sbMime,0,0,0,0)
If success = 0
Debug CkImap::ckLastErrorText(imapDest)
CkImap::ckDispose(imapSrc)
CkImap::ckDispose(imapDest)
CkStringBuilder::ckDispose(sbMime)
ProcedureReturn
EndIf
Next
; Disconnect from the IMAP servers.
success = CkImap::ckDisconnect(imapSrc)
success = CkImap::ckDisconnect(imapDest)
CkImap::ckDispose(imapSrc)
CkImap::ckDispose(imapDest)
CkStringBuilder::ckDispose(sbMime)
ProcedureReturn
EndProcedure