Sample code for 30+ languages & platforms
PowerBuilder

Fetch a MessageSet into an EmailBundle

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchMsgSet method, which downloads the messages identified by a MessageSet and appends them to an EmailBundle. The arguments are headersOnly, the MessageSet, and the EmailBundle (which is not cleared). This example searches for unseen messages and downloads the whole set.

Background: This is the natural pairing with QueryMbx: search to get a MessageSet of matching identifiers, then download exactly those messages in one call. Fetching a set is far more efficient than looping one message at a time, and it returns an EmailBundle you iterate with MessageCount and EmailAt. Pass headersOnly = true to pull just headers for a fast preview list.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
oleobject loo_MsgSet
oleobject loo_Bundle
integer n
oleobject loo_Email
integer i

li_Success = 0

//  Demonstrates the Imap.FetchMsgSet method, which downloads the messages identified by a
//  MessageSet and appends them to an EmailBundle.  The 1st argument is headersOnly, the 2nd
//  is the MessageSet, and the 3rd is the EmailBundle (which is not cleared).

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

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

li_Success = loo_Imap.Login("user@example.com","myPassword")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.SelectMailbox("Inbox")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

//  Find the messages to download (here, the unseen messages by UID).
loo_MsgSet = create oleobject
li_rc = loo_MsgSet.ConnectToNewObject("Chilkat.MessageSet")

li_Success = loo_Imap.QueryMbx("UNSEEN",1,loo_MsgSet)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MsgSet
    return
end if

//  Download the messages in the set (full bodies) into a bundle.
loo_Bundle = create oleobject
li_rc = loo_Bundle.ConnectToNewObject("Chilkat.EmailBundle")

li_Success = loo_Imap.FetchMsgSet(0,loo_MsgSet,loo_Bundle)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MsgSet
    destroy loo_Bundle
    return
end if

n = loo_Bundle.MessageCount
Write-Debug "Fetched " + string(n) + " messages."
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

for i = 0 to n - 1
    li_Success = loo_Bundle.EmailAt(i,loo_Email)
    Write-Debug loo_Email.Subject
next

li_Success = loo_Imap.Disconnect()
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MsgSet
    destroy loo_Bundle
    destroy loo_Email
    return
end if



destroy loo_Imap
destroy loo_MsgSet
destroy loo_Bundle
destroy loo_Email