Sample code for 30+ languages & platforms
PowerBuilder

Download MIME Source of Emails in IMAP Mailbox

Demonstrates how to download the MIME source for emails on an IMAP server.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_NumMessages
oleobject loo_SbMime
integer li_SeqNum

li_Success = 0

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

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

// Connect to an IMAP server.
// Use TLS
loo_Imap.Ssl = 1
loo_Imap.Port = 993
li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

// Login
li_Success = loo_Imap.Login("myLogin","myPassword")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

// Select an IMAP mailbox
li_Success = loo_Imap.SelectMailbox("Inbox")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

// The NumMessages property contains the number of messages in the selected mailbox.
li_NumMessages = loo_Imap.NumMessages
if li_NumMessages = 0 then
    Write-Debug "No messages exist in the Inbox."
    destroy loo_Imap
    return
end if

loo_SbMime = create oleobject
li_rc = loo_SbMime.ConnectToNewObject("Chilkat.StringBuilder")

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

    // The MIME source of the downloaded email is contained in sbMime.
next

// Disconnect from the IMAP server.
li_Success = loo_Imap.Disconnect()


destroy loo_Imap
destroy loo_SbMime