Sample code for 30+ languages & platforms
Visual Basic 6.0

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 Visual Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

success = 0

' This example assumes the Chilkat API has already been unlocked.
' See Global Unlock Sample for example code.

Dim imap As New ChilkatImap

' Connect to the IMAP server using SSL/TLS on the standard secure IMAP port.
imap.Ssl = 1
imap.Port = 993
success = imap.Connect("imap.example.com")
If (success = 0) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

' Authenticate with the IMAP server.
success = imap.Login("****","****")
If (success = 0) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

' Select the mailbox (folder) to access.
success = imap.SelectMailbox("Inbox")
If (success = 0) Then
    Debug.Print imap.LastErrorText
    Exit Sub
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.
Dim fetchUids As Long
fetchUids = 1
Dim messageSet As New MessageSet
success = imap.QueryMbx("ALL",fetchUids,messageSet)
If (success = 0) Then
    Debug.Print imap.LastErrorText
    Exit Sub
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.
Dim bundle As New ChilkatEmailBundle
Dim headersOnly As Long
headersOnly = 1
success = imap.FetchMsgSet(headersOnly,messageSet,bundle)
If (success = 0) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

' Iterate over each email in the bundle and display summary information.
Dim email As New ChilkatEmail
Dim name As String
Dim addr As String
Dim i As Long
i = 0
Dim j As Long
j = 0
Do While i < bundle.MessageCount
    success = bundle.EmailAt(i,email)

    ' Display the sender and subject line.
    Debug.Print email.From
    Debug.Print email.Subject

    ' Display all "To" recipients.
    j = 0
    Do While j < email.NumTo
        Debug.Print "TO: " & email.GetToName(j) & ", " & email.GetToAddr(j)
        j = j + 1
    Loop

    ' Display all "CC" recipients.
    j = 0
    Do While j < email.NumCC
        Debug.Print "CC: " & email.GetCcName(j) & ", " & 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.
    Debug.Print 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.
    Dim numAttach As Long
    numAttach = imap.GetMailNumAttach(email)

    j = 0
    Do While j < numAttach

        ' Display the attachment filename.
        Debug.Print imap.GetMailAttachFilename(email,j)

        ' Display the attachment size in bytes.
        Dim attachSize As Long
        attachSize = imap.GetMailAttachSize(email,j)
        Debug.Print "    size = " & attachSize & " bytes"

        j = j + 1
    Loop

    Debug.Print "--"

    i = i + 1
Loop

' Disconnect from the IMAP server.
success = imap.Disconnect()