Sample code for 30+ languages & platforms
AutoIt

Copy Email from one IMAP Account to Another

See more IMAP Examples

Demonstrates how to copy the email in a mailbox from one account to another.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

$oImapSrc = ObjCreate("Chilkat.Imap")

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

; Connect to our source IMAP server.
$oImapSrc.Ssl = True
$oImapSrc.Port = 993
$bSuccess = $oImapSrc.Connect("MY-IMAP-DOMAIN")
If ($bSuccess <> True) Then
    ConsoleWrite($oImapSrc.LastErrorText & @CRLF)
    Exit
EndIf

; Login to the source IMAP server
$bSuccess = $oImapSrc.Login("MY-IMAP-LOGIN","MY-IMAP-PASSWORD")
If ($bSuccess <> True) Then
    ConsoleWrite($oImapSrc.LastErrorText & @CRLF)
    Exit
EndIf

$oImapDest = ObjCreate("Chilkat.Imap")

; Connect to our destination IMAP server.
$oImapDest.Ssl = True
$oImapDest.Port = 993
$bSuccess = $oImapDest.Connect("MY-IMAP-DOMAIN2")
If ($bSuccess <> True) Then
    ConsoleWrite($oImapDest.LastErrorText & @CRLF)
    Exit
EndIf

; Login to the destination IMAP server
$bSuccess = $oImapDest.Login("MY-IMAP-LOGIN2","MY-IMAP-PASSWORD2")
If ($bSuccess <> True) Then
    ConsoleWrite($oImapDest.LastErrorText & @CRLF)
    Exit
EndIf

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

Local $bFetchUids = True

; Get the set of UIDs for all emails on the source server.
Local $oMset = $oImapSrc.Search("ALL",$bFetchUids)
If ($oImapSrc.LastMethodSuccess <> True) Then
    ConsoleWrite($oImapSrc.LastErrorText & @CRLF)
    Exit
EndIf

; Load the complete set of UIDs that were previously copied.
; We dont' want to copy any of these to the destination.
$oFac = ObjCreate("Chilkat.FileAccess")
$oMsetAlreadyCopied = ObjCreate("Chilkat.MessageSet")
Local $strMsgSet = $oFac.ReadEntireTextFile("qa_cache/saAlreadyLoaded.txt","utf-8")
If ($oFac.LastMethodSuccess = True) Then
    $oMsetAlreadyCopied.FromCompactString($strMsgSet)
EndIf

Local $iNumUids = $oMset.Count
$oSbFlags = ObjCreate("Chilkat.StringBuilder")

Local $i = 0
While $i < $iNumUids

    ; If this UID was not already copied...
Local $iUid = $oMset.GetId($i)
    If (Not $oMsetAlreadyCopied.ContainsId($iUid)) Then

        ConsoleWrite("copying " & $iUid & "..." & @CRLF)

        ; Get the flags.
Local $sFlags = $oImapSrc.FetchFlags($iUid,True)
        If ($oImapSrc.LastMethodSuccess = False) Then
            ConsoleWrite($oImapSrc.LastErrorText & @CRLF)
            Exit
        EndIf

        $oSbFlags.SetString($sFlags)

        ; Get the MIME of this email from the source.
Local $sMimeStr = $oImapSrc.FetchSingleAsMime($iUid,True)
        If ($oImapSrc.LastMethodSuccess = False) Then
            ConsoleWrite($oImapSrc.LastErrorText & @CRLF)
            Exit
        EndIf

Local $bSeen = $oSbFlags.Contains("\Seen",False)
Local $bFlagged = $oSbFlags.Contains("\Flagged",False)
Local $bAnswered = $oSbFlags.Contains("\Answered",False)
Local $bDraft = $oSbFlags.Contains("\Draft",False)

        $bSuccess = $oImapDest.AppendMimeWithFlags("Inbox",$sMimeStr,$bSeen,$bFlagged,$bAnswered,$bDraft)
        If ($bSuccess <> True) Then
            ConsoleWrite($oImapDest.LastErrorText & @CRLF)
            Exit
        EndIf

        ; Update msetAlreadyCopied with the uid just copied.
        $oMsetAlreadyCopied.InsertId $iUid

        ; Save at every iteration just in case there's a failure..
        $strMsgSet = $oMsetAlreadyCopied.ToCompactString()
        $oFac.WriteEntireTextFile("qa_cache/saAlreadyLoaded.txt",$strMsgSet,"utf-8",False)
    EndIf

    $i = $i + 1
Wend

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