Sample code for 30+ languages & platforms
PowerBuilder

Read IMAP Email Headers

This example demonstrates how to connect to an IMAP server and download only the email headers for all messages in a mailbox. Downloading headers only is much faster and more efficient than downloading complete emails because the message bodies and attachment contents are not retrieved.

The example shows how to:

  • Connect to an IMAP server using SSL/TLS
  • Authenticate and select a mailbox
  • Query for all messages in the mailbox
  • Fetch header-only email information
  • Display sender, subject, recipients, and total message size
  • Retrieve attachment metadata such as filenames and sizes without downloading the actual attachments

This technique is useful for quickly scanning large mailboxes, building message summaries, or inspecting attachment information while minimizing bandwidth and memory usage.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_FetchUids
oleobject loo_MessageSet
oleobject loo_Bundle
integer li_HeadersOnly
oleobject loo_Email
string ls_Name
string ls_Addr
integer i
integer j
integer li_NumAttach
integer li_AttachSize

li_Success = 0

li_Success = 0

// This example assumes the Chilkat API has already been unlocked.
// See Global Unlock Sample for example 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 the IMAP server using SSL/TLS on the standard secure IMAP port.
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

// Authenticate with the IMAP server.
li_Success = loo_Imap.Login("****","****")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

// Select the mailbox (folder) to access.
li_Success = loo_Imap.SelectMailbox("Inbox")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

// Get the identifiers for all messages in the selected mailbox.
// Setting fetchUids = cktrue causes IMAP UIDs to be returned.
// If ckfalse, IMAP sequence numbers are returned instead.
li_FetchUids = 1
loo_MessageSet = create oleobject
li_rc = loo_MessageSet.ConnectToNewObject("Chilkat.MessageSet")

li_Success = loo_Imap.QueryMbx("ALL",li_FetchUids,loo_MessageSet)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MessageSet
    return
end if

// Download only the email headers for the messages in the set.
// This is much faster than downloading full emails because the
// message bodies and attachment contents are not retrieved.
// 
// Even though the attachments themselves are not downloaded,
// Chilkat can still provide information such as attachment
// filenames, attachment sizes, and the total message size.
loo_Bundle = create oleobject
li_rc = loo_Bundle.ConnectToNewObject("Chilkat.EmailBundle")

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

// Iterate over each email in the bundle and display summary information.
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

i = 0
j = 0
do while i < loo_Bundle.MessageCount
    loo_Bundle.EmailAt(i,loo_Email)

    // Display the sender and subject line.
    Write-Debug loo_Email.From
    Write-Debug loo_Email.Subject

    // Display all "To" recipients.
    j = 0
    do while j < loo_Email.NumTo
        Write-Debug "TO: " + loo_Email.GetToName(j) + ", " + loo_Email.GetToAddr(j)
        j = j + 1
    loop

    // Display all "CC" recipients.
    j = 0
    do while j < loo_Email.NumCC
        Write-Debug "CC: " + loo_Email.GetCcName(j) + ", " + loo_Email.GetCcAddr(j)
        j = j + 1
    loop

    // Display the total size of the email message, including
    // the body and all attachments, even though only headers
    // were downloaded.
    Write-Debug string(loo_Email.Size)

    // When full emails are downloaded, attachment information
    // is accessed using the email.NumAttachments property and
    // the email.GetMailAttach* methods.
    // 
    // However, because this example downloaded headers only,
    // attachment metadata must be obtained using the IMAP object.
    li_NumAttach = loo_Imap.GetMailNumAttach(loo_Email)

    j = 0
    do while j < li_NumAttach

        // Display the attachment filename.
        Write-Debug loo_Imap.GetMailAttachFilename(loo_Email,j)

        // Display the attachment size in bytes.
        li_AttachSize = loo_Imap.GetMailAttachSize(loo_Email,j)
        Write-Debug "    size = " + string(li_AttachSize) + " bytes"

        j = j + 1
    loop

    Write-Debug "--"

    i = i + 1
loop

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


destroy loo_Imap
destroy loo_MessageSet
destroy loo_Bundle
destroy loo_Email