Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_ImapSrc
oleobject loo_ImapDest
integer li_FetchUids
oleobject loo_Mset
oleobject loo_Fac
oleobject loo_MsetAlreadyCopied
string ls_StrMsgSet
integer li_NumUids
oleobject loo_SbFlags
integer i
integer li_Uid
string ls_Flags
string ls_MimeStr
integer li_Seen
integer li_Flagged
integer li_Answered
integer li_Draft

li_Success = 0

loo_ImapSrc = create oleobject
li_rc = loo_ImapSrc.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_ImapSrc
    MessageBox("Error","Connecting to COM object failed")
    return
end if

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

// Connect to our source IMAP server.
loo_ImapSrc.Ssl = 1
loo_ImapSrc.Port = 993
li_Success = loo_ImapSrc.Connect("MY-IMAP-DOMAIN")
if li_Success <> 1 then
    Write-Debug loo_ImapSrc.LastErrorText
    destroy loo_ImapSrc
    return
end if

// Login to the source IMAP server
li_Success = loo_ImapSrc.Login("MY-IMAP-LOGIN","MY-IMAP-PASSWORD")
if li_Success <> 1 then
    Write-Debug loo_ImapSrc.LastErrorText
    destroy loo_ImapSrc
    return
end if

loo_ImapDest = create oleobject
li_rc = loo_ImapDest.ConnectToNewObject("Chilkat.Imap")

// Connect to our destination IMAP server.
loo_ImapDest.Ssl = 1
loo_ImapDest.Port = 993
li_Success = loo_ImapDest.Connect("MY-IMAP-DOMAIN2")
if li_Success <> 1 then
    Write-Debug loo_ImapDest.LastErrorText
    destroy loo_ImapSrc
    destroy loo_ImapDest
    return
end if

// Login to the destination IMAP server
li_Success = loo_ImapDest.Login("MY-IMAP-LOGIN2","MY-IMAP-PASSWORD2")
if li_Success <> 1 then
    Write-Debug loo_ImapDest.LastErrorText
    destroy loo_ImapSrc
    destroy loo_ImapDest
    return
end if

// Select a source IMAP mailbox on the source IMAP server
li_Success = loo_ImapSrc.SelectMailbox("Inbox")
if li_Success <> 1 then
    Write-Debug loo_ImapSrc.LastErrorText
    destroy loo_ImapSrc
    destroy loo_ImapDest
    return
end if

li_FetchUids = 1

// Get the set of UIDs for all emails on the source server.
loo_Mset = loo_ImapSrc.Search("ALL",li_FetchUids)
if loo_ImapSrc.LastMethodSuccess <> 1 then
    Write-Debug loo_ImapSrc.LastErrorText
    destroy loo_ImapSrc
    destroy loo_ImapDest
    return
end if

// Load the complete set of UIDs that were previously copied.
// We dont' want to copy any of these to the destination.
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")

loo_MsetAlreadyCopied = create oleobject
li_rc = loo_MsetAlreadyCopied.ConnectToNewObject("Chilkat.MessageSet")

ls_StrMsgSet = loo_Fac.ReadEntireTextFile("qa_cache/saAlreadyLoaded.txt","utf-8")
if loo_Fac.LastMethodSuccess = 1 then
    loo_MsetAlreadyCopied.FromCompactString(ls_StrMsgSet)
end if

li_NumUids = loo_Mset.Count
loo_SbFlags = create oleobject
li_rc = loo_SbFlags.ConnectToNewObject("Chilkat.StringBuilder")

i = 0
do while i < li_NumUids

    // If this UID was not already copied...
    li_Uid = loo_Mset.GetId(i)
    if not loo_MsetAlreadyCopied.ContainsId(li_Uid) then

        Write-Debug "copying " + string(li_Uid) + "..."

        // Get the flags.
        ls_Flags = loo_ImapSrc.FetchFlags(li_Uid,1)
        if loo_ImapSrc.LastMethodSuccess = 0 then
            Write-Debug loo_ImapSrc.LastErrorText
            destroy loo_ImapSrc
            destroy loo_ImapDest
            destroy loo_Fac
            destroy loo_MsetAlreadyCopied
            destroy loo_SbFlags
            return
        end if

        loo_SbFlags.SetString(ls_Flags)

        // Get the MIME of this email from the source.
        ls_MimeStr = loo_ImapSrc.FetchSingleAsMime(li_Uid,1)
        if loo_ImapSrc.LastMethodSuccess = 0 then
            Write-Debug loo_ImapSrc.LastErrorText
            destroy loo_ImapSrc
            destroy loo_ImapDest
            destroy loo_Fac
            destroy loo_MsetAlreadyCopied
            destroy loo_SbFlags
            return
        end if

        li_Seen = loo_SbFlags.Contains("\Seen",0)
        li_Flagged = loo_SbFlags.Contains("\Flagged",0)
        li_Answered = loo_SbFlags.Contains("\Answered",0)
        li_Draft = loo_SbFlags.Contains("\Draft",0)

        li_Success = loo_ImapDest.AppendMimeWithFlags("Inbox",ls_MimeStr,li_Seen,li_Flagged,li_Answered,li_Draft)
        if li_Success <> 1 then
            Write-Debug loo_ImapDest.LastErrorText
            destroy loo_ImapSrc
            destroy loo_ImapDest
            destroy loo_Fac
            destroy loo_MsetAlreadyCopied
            destroy loo_SbFlags
            return
        end if

        // Update msetAlreadyCopied with the uid just copied.
        loo_MsetAlreadyCopied.InsertId(li_Uid)

        // Save at every iteration just in case there's a failure..
        ls_StrMsgSet = loo_MsetAlreadyCopied.ToCompactString()
        loo_Fac.WriteEntireTextFile("qa_cache/saAlreadyLoaded.txt",ls_StrMsgSet,"utf-8",0)
    end if

    i = i + 1
loop
destroy loo_Mset

// Disconnect from the IMAP servers.
li_Success = loo_ImapSrc.Disconnect()
li_Success = loo_ImapDest.Disconnect()


destroy loo_ImapSrc
destroy loo_ImapDest
destroy loo_Fac
destroy loo_MsetAlreadyCopied
destroy loo_SbFlags