Xojo Plugin
Xojo Plugin
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 Xojo Plugin Downloads
Dim success As Boolean
success = False
success = False
// This example assumes the Chilkat API has already been unlocked.
// See Global Unlock Sample for example code.
Dim imap As New Chilkat.Imap
// Connect to the IMAP server using SSL/TLS on the standard secure IMAP port.
imap.Ssl = True
imap.Port = 993
success = imap.Connect("imap.example.com")
If (success = False) Then
System.DebugLog(imap.LastErrorText)
Return
End If
// Authenticate with the IMAP server.
success = imap.Login("****","****")
If (success = False) Then
System.DebugLog(imap.LastErrorText)
Return
End If
// Select the mailbox (folder) to access.
success = imap.SelectMailbox("Inbox")
If (success = False) Then
System.DebugLog(imap.LastErrorText)
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.
Dim fetchUids As Boolean
fetchUids = True
Dim messageSet As New Chilkat.MessageSet
success = imap.QueryMbx("ALL",fetchUids,messageSet)
If (success = False) Then
System.DebugLog(imap.LastErrorText)
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.
Dim bundle As New Chilkat.EmailBundle
Dim headersOnly As Boolean
headersOnly = True
success = imap.FetchMsgSet(headersOnly,messageSet,bundle)
If (success = False) Then
System.DebugLog(imap.LastErrorText)
Return
End If
// Iterate over each email in the bundle and display summary information.
Dim email As New Chilkat.Email
Dim name As String
Dim addr As String
Dim i As Int32
i = 0
Dim j As Int32
j = 0
While i < bundle.MessageCount
success = bundle.EmailAt(i,email)
// Display the sender and subject line.
System.DebugLog(email.From)
System.DebugLog(email.Subject)
// Display all "To" recipients.
j = 0
While j < email.NumTo
System.DebugLog("TO: " + email.GetToName(j) + ", " + email.GetToAddr(j))
j = j + 1
Wend
// Display all "CC" recipients.
j = 0
While j < email.NumCC
System.DebugLog("CC: " + email.GetCcName(j) + ", " + email.GetCcAddr(j))
j = j + 1
Wend
// Display the total size of the email message, including
// the body and all attachments, even though only headers
// were downloaded.
System.DebugLog(Str(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 Int32
numAttach = imap.GetMailNumAttach(email)
j = 0
While j < numAttach
// Display the attachment filename.
System.DebugLog(imap.GetMailAttachFilename(email,j))
// Display the attachment size in bytes.
Dim attachSize As Int32
attachSize = imap.GetMailAttachSize(email,j)
System.DebugLog(" size = " + Str(attachSize) + " bytes")
j = j + 1
Wend
System.DebugLog("--")
i = i + 1
Wend
// Disconnect from the IMAP server.
success = imap.Disconnect()