Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.0.0+

Preview POP3 messages and download a complete email

See more Email Object Examples

Read a short preview for each of up to five messages into an EmailBundle, print their subjects, and download the complete first entry into a separate Email. Configure a POP3 account that supports UIDL and TOP. All messages stay on the server.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Mail
oleobject loo_Previews
oleobject loo_Header
long i
oleobject loo_Email

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

loo_Mail = create oleobject
li_rc = loo_Mail.ConnectToNewObject("Chilkat.MailMan")
if li_rc < 0 then
    destroy loo_Mail
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Mail.MailHost = "pop.example.com"
loo_Mail.MailPort = 995
loo_Mail.PopSsl = 1
loo_Mail.RequireSslCertVerify = 1
loo_Mail.PopUsername = "MAIL_USER"
loo_Mail.PopPassword = "MAIL_PASSWORD"
loo_Previews = create oleobject
li_rc = loo_Previews.ConnectToNewObject("Chilkat.EmailBundle")

//  Keep the messages and request minimal previews. Range indexes are zero-based and inclusive.
li_Success = loo_Mail.FetchRange(1,1,0,0,4,loo_Previews)
if li_Success <> 1 then
    Write-Debug loo_Mail.LastErrorText
    destroy loo_Mail
    destroy loo_Previews
    return
end if

if loo_Previews.MessageCount = 0 then
    Write-Debug "There are no messages to preview."
    li_Success = loo_Mail.Pop3EndSession()
    if li_Success <> 1 then
        Write-Debug loo_Mail.LastErrorText
        destroy loo_Mail
        destroy loo_Previews
        return
    end if

    destroy loo_Mail
    destroy loo_Previews
    return
end if

loo_Header = create oleobject
li_rc = loo_Header.ConnectToNewObject("Chilkat.Email")

i = 0
do while (i < loo_Previews.MessageCount)
    li_Success = loo_Previews.EmailAt(i,loo_Header)
    if li_Success <> 1 then
        Write-Debug loo_Previews.LastErrorText
        destroy loo_Mail
        destroy loo_Previews
        destroy loo_Header
        return
    end if

    Write-Debug loo_Header.Uidl + "  " + loo_Header.Subject
    i = i + 1
loop
//  A UI could use its selected row here. FetchFull uses the preview's POP3 identity.
li_Success = loo_Previews.EmailAt(0,loo_Header)
if li_Success <> 1 then
    Write-Debug loo_Previews.LastErrorText
    destroy loo_Mail
    destroy loo_Previews
    destroy loo_Header
    return
end if

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

li_Success = loo_Mail.FetchFull(loo_Header,loo_Email)
if li_Success <> 1 then
    Write-Debug loo_Mail.LastErrorText
    destroy loo_Mail
    destroy loo_Previews
    destroy loo_Header
    destroy loo_Email
    return
end if

li_Success = loo_Mail.Pop3EndSession()
if li_Success <> 1 then
    Write-Debug loo_Mail.LastErrorText
    destroy loo_Mail
    destroy loo_Previews
    destroy loo_Header
    destroy loo_Email
    return
end if

Write-Debug "Full message: " + loo_Email.Subject + "  Files: " + string(loo_Email.NumAttachments)

li_Success = loo_Email.SaveEml("pop-received.eml")
if li_Success <> 1 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Mail
    destroy loo_Previews
    destroy loo_Header
    destroy loo_Email
    return
end if

Write-Debug "Saved pop-received.eml."


destroy loo_Mail
destroy loo_Previews
destroy loo_Header
destroy loo_Email