Sample code for 30+ languages & platforms
Visual Basic 6.0

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 Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

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

Dim imapSrc As New ChilkatImap

' Connect to our source IMAP server.
success = imapSrc.Connect("imap.example.com")
If (success = 0) Then
    Debug.Print imapSrc.LastErrorText
    Exit Sub
End If

' Login to the source IMAP server
success = imapSrc.Login("admin@chilkatsoft.com","myPassword")
If (success = 0) Then
    Debug.Print imapSrc.LastErrorText
    Exit Sub
End If

Dim imapDest As New ChilkatImap

' Connect to our destination IMAP server.
success = imapDest.Connect("mail.example-code.com")
If (success = 0) Then
    Debug.Print imapDest.LastErrorText
    Exit Sub
End If

' Login to the destination IMAP server
success = imapDest.Login("myLogin","myPassword")
If (success = 0) Then
    Debug.Print imapDest.LastErrorText
    Exit Sub
End If

' Select an IMAP mailbox on the source IMAP server
success = imapSrc.SelectMailbox("Inbox")
If (success = 0) Then
    Debug.Print imapSrc.LastErrorText
    Exit Sub
End If

' After selecting a mailbox, the NumMessages property will
' be updated to reflect the total number of emails in the mailbox:
Debug.Print 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 ChilkatStringBuilder
Dim seqNum As Long
For seqNum = 1 To 10
    sbMime.Clear 
    success = imapSrc.FetchSingleAsMimeSb(seqNum,0,sbMime)
    If (success = 0) Then
        Debug.Print imapSrc.LastErrorText
        Exit Sub
    End If

    success = imapDest.AppendMimeWithFlagsSb("Inbox",sbMime,0,0,0,0)
    If (success = 0) Then
        Debug.Print imapDest.LastErrorText
        Exit Sub
    End If

Next

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