Sample code for 30+ languages & platforms
AutoIt Requires Chilkat v11.0.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 AutoIt Downloads

AutoIt
Local $bSuccess = False

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

$oImapSrc = ObjCreate("Chilkat.Imap")

;  Connect to our source IMAP server.
$bSuccess = $oImapSrc.Connect("imap.example.com")
If ($bSuccess = False) Then
    ConsoleWrite($oImapSrc.LastErrorText & @CRLF)
    Exit
EndIf

;  Login to the source IMAP server
$bSuccess = $oImapSrc.Login("admin@chilkatsoft.com","myPassword")
If ($bSuccess = False) Then
    ConsoleWrite($oImapSrc.LastErrorText & @CRLF)
    Exit
EndIf

$oImapDest = ObjCreate("Chilkat.Imap")

;  Connect to our destination IMAP server.
$bSuccess = $oImapDest.Connect("mail.example-code.com")
If ($bSuccess = False) Then
    ConsoleWrite($oImapDest.LastErrorText & @CRLF)
    Exit
EndIf

;  Login to the destination IMAP server
$bSuccess = $oImapDest.Login("myLogin","myPassword")
If ($bSuccess = False) Then
    ConsoleWrite($oImapDest.LastErrorText & @CRLF)
    Exit
EndIf

;  Select an IMAP mailbox on the source IMAP server
$bSuccess = $oImapSrc.SelectMailbox("Inbox")
If ($bSuccess = False) Then
    ConsoleWrite($oImapSrc.LastErrorText & @CRLF)
    Exit
EndIf

;  After selecting a mailbox, the NumMessages property will
;  be updated to reflect the total number of emails in the mailbox:
ConsoleWrite($oImapSrc.NumMessages & @CRLF)

;  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
$oSbMime = ObjCreate("Chilkat.StringBuilder")
Local $iSeqNum
For $iSeqNum = 1 To 10
    $oSbMime.Clear 
    $bSuccess = $oImapSrc.FetchSingleAsMimeSb($iSeqNum,False,$oSbMime)
    If ($bSuccess = False) Then
        ConsoleWrite($oImapSrc.LastErrorText & @CRLF)
        Exit
    EndIf

    $bSuccess = $oImapDest.AppendMimeWithFlagsSb("Inbox",$oSbMime,False,False,False,False)
    If ($bSuccess = False) Then
        ConsoleWrite($oImapDest.LastErrorText & @CRLF)
        Exit
    EndIf

Next

;  Disconnect from the IMAP servers.
$bSuccess = $oImapSrc.Disconnect()
$bSuccess = $oImapDest.Disconnect()