Sample code for 30+ languages & platforms
VB.NET

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 VB.NET Downloads

VB.NET
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 New Chilkat.Imap

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


' Login to the source IMAP server
success = imapSrc.Login("admin@chilkatsoft.com","myPassword")
If (success = False) Then
    Debug.WriteLine(imapSrc.LastErrorText)
    Exit Sub
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
    Debug.WriteLine(imapDest.LastErrorText)
    Exit Sub
End If


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


' Select an IMAP mailbox on the source IMAP server
success = imapSrc.SelectMailbox("Inbox")
If (success = False) Then
    Debug.WriteLine(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.WriteLine(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 Integer
For seqNum = 1 To 10
    sbMime.Clear()
    success = imapSrc.FetchSingleAsMimeSb(seqNum,False,sbMime)
    If (success = False) Then
        Debug.WriteLine(imapSrc.LastErrorText)
        Exit Sub
    End If


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

Next

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