Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_ImapSrc
oleobject loo_ImapDest
oleobject loo_SbMime
integer li_SeqNum

li_Success = 0

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

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

// Connect to our source IMAP server.
li_Success = loo_ImapSrc.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_ImapSrc.LastErrorText
    destroy loo_ImapSrc
    return
end if

// Login to the source IMAP server
li_Success = loo_ImapSrc.Login("admin@chilkatsoft.com","myPassword")
if li_Success = 0 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.
li_Success = loo_ImapDest.Connect("mail.example-code.com")
if li_Success = 0 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("myLogin","myPassword")
if li_Success = 0 then
    Write-Debug loo_ImapDest.LastErrorText
    destroy loo_ImapSrc
    destroy loo_ImapDest
    return
end if

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

// After selecting a mailbox, the NumMessages property will
// be updated to reflect the total number of emails in the mailbox:
Write-Debug string(loo_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
loo_SbMime = create oleobject
li_rc = loo_SbMime.ConnectToNewObject("Chilkat.StringBuilder")

for seqNum = 1 to 10
    loo_SbMime.Clear()
    li_Success = loo_ImapSrc.FetchSingleAsMimeSb(li_SeqNum,0,loo_SbMime)
    if li_Success = 0 then
        Write-Debug loo_ImapSrc.LastErrorText
        destroy loo_ImapSrc
        destroy loo_ImapDest
        destroy loo_SbMime
        return
    end if

    li_Success = loo_ImapDest.AppendMimeWithFlagsSb("Inbox",loo_SbMime,0,0,0,0)
    if li_Success = 0 then
        Write-Debug loo_ImapDest.LastErrorText
        destroy loo_ImapSrc
        destroy loo_ImapDest
        destroy loo_SbMime
        return
    end if

next

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


destroy loo_ImapSrc
destroy loo_ImapDest
destroy loo_SbMime