Sample code for 30+ languages & platforms
Xojo Plugin

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 Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

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

Dim imapSrc As New Chilkat.Imap

// Connect to our source IMAP server.
success = imapSrc.Connect("imap.example.com")
If (success = False) Then
    System.DebugLog(imapSrc.LastErrorText)
    Return
End If

// Login to the source IMAP server
success = imapSrc.Login("admin@chilkatsoft.com","myPassword")
If (success = False) Then
    System.DebugLog(imapSrc.LastErrorText)
    Return
End If

Dim imapDest As New Chilkat.Imap

// Connect to our destination IMAP server.
success = imapDest.Connect("mail.example-code.com")
If (success = False) Then
    System.DebugLog(imapDest.LastErrorText)
    Return
End If

// Login to the destination IMAP server
success = imapDest.Login("myLogin","myPassword")
If (success = False) Then
    System.DebugLog(imapDest.LastErrorText)
    Return
End If

// Select an IMAP mailbox on the source IMAP server
success = imapSrc.SelectMailbox("Inbox")
If (success = False) Then
    System.DebugLog(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:
System.DebugLog(Str(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 New Chilkat.StringBuilder
Dim seqNum As Int32
For seqNum = 1 To 10
    sbMime.Clear 
    success = imapSrc.FetchSingleAsMimeSb(seqNum,False,sbMime)
    If (success = False) Then
        System.DebugLog(imapSrc.LastErrorText)
        Return
    End If

    success = imapDest.AppendMimeWithFlagsSb("Inbox",sbMime,False,False,False,False)
    If (success = False) Then
        System.DebugLog(imapDest.LastErrorText)
        Return
    End If

Next

// Disconnect from the IMAP servers.
success = imapSrc.Disconnect()
success = imapDest.Disconnect()