B4X Requires Chilkat v11.0.0+
B4X
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 B4X Downloads
Dim success As Boolean = False
' This example requires the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.
Dim imapSrc As ChilkatImap
imapSrc.Initialize("imapSrc")
' Connect to our source IMAP server.
success = imapSrc.Connect("imap.example.com")
If success = False Then
Log(imapSrc.LastErrorText)
Return
End If
' Login to the source IMAP server
success = imapSrc.Login("admin@chilkatsoft.com", "myPassword")
If success = False Then
Log(imapSrc.LastErrorText)
Return
End If
Dim imapDest As ChilkatImap
imapDest.Initialize("imapDest")
' Connect to our destination IMAP server.
success = imapDest.Connect("mail.example-code.com")
If success = False Then
Log(imapDest.LastErrorText)
Return
End If
' Login to the destination IMAP server
success = imapDest.Login("myLogin", "myPassword")
If success = False Then
Log(imapDest.LastErrorText)
Return
End If
' Select an IMAP mailbox on the source IMAP server
success = imapSrc.SelectMailbox("Inbox")
If success = False Then
Log(imapSrc.LastErrorText)
Return
End If
' After selecting a mailbox, the NumMessages property will
' be updated to reflect the total number of emails in the mailbox:
Log(imapSrc.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
Dim sbMime As ChilkatStringBuilder
sbMime.Initialize
Dim seqNum As Int
For seqNum = 1 To 10
sbMime.Clear
success = imapSrc.FetchSingleAsMimeSb(seqNum, False, sbMime)
If success = False Then
Log(imapSrc.LastErrorText)
Return
End If
success = imapDest.AppendMimeWithFlagsSb("Inbox", sbMime, False, False, False, False)
If success = False Then
Log(imapDest.LastErrorText)
Return
End If
Next
' Disconnect from the IMAP servers.
success = imapSrc.Disconnect
success = imapDest.Disconnect