Visual FoxPro
Visual FoxPro
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 FoxPro Downloads
LOCAL lnSuccess
LOCAL loImap
LOCAL lnFetchUids
LOCAL loMessageSet
LOCAL loBundle
LOCAL lnHeadersOnly
LOCAL loEmail
LOCAL lcName
LOCAL lcAddr
LOCAL i
LOCAL j
LOCAL lnNumAttach
LOCAL lnAttachSize
lnSuccess = 0
lnSuccess = 0
* This example assumes the Chilkat API has already been unlocked.
* See Global Unlock Sample for example code.
loImap = CreateObject('Chilkat.Imap')
* Connect to the IMAP server using SSL/TLS on the standard secure IMAP port.
loImap.Ssl = 1
loImap.Port = 993
lnSuccess = loImap.Connect("imap.example.com")
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
CANCEL
ENDIF
* Authenticate with the IMAP server.
lnSuccess = loImap.Login("****","****")
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
CANCEL
ENDIF
* Select the mailbox (folder) to access.
lnSuccess = loImap.SelectMailbox("Inbox")
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
CANCEL
ENDIF
* 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.
lnFetchUids = 1
loMessageSet = CreateObject('Chilkat.MessageSet')
lnSuccess = loImap.QueryMbx("ALL",lnFetchUids,loMessageSet)
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
RELEASE loMessageSet
CANCEL
ENDIF
* 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.
loBundle = CreateObject('Chilkat.EmailBundle')
lnHeadersOnly = 1
lnSuccess = loImap.FetchMsgSet(lnHeadersOnly,loMessageSet,loBundle)
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
RELEASE loMessageSet
RELEASE loBundle
CANCEL
ENDIF
* Iterate over each email in the bundle and display summary information.
loEmail = CreateObject('Chilkat.Email')
i = 0
j = 0
DO WHILE i < loBundle.MessageCount
loBundle.EmailAt(i,loEmail)
* Display the sender and subject line.
? loEmail.From
? loEmail.Subject
* Display all "To" recipients.
j = 0
DO WHILE j < loEmail.NumTo
? "TO: " + loEmail.GetToName(j) + ", " + loEmail.GetToAddr(j)
j = j + 1
ENDDO
* Display all "CC" recipients.
j = 0
DO WHILE j < loEmail.NumCC
? "CC: " + loEmail.GetCcName(j) + ", " + loEmail.GetCcAddr(j)
j = j + 1
ENDDO
* Display the total size of the email message, including
* the body and all attachments, even though only headers
* were downloaded.
? STR(loEmail.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.
lnNumAttach = loImap.GetMailNumAttach(loEmail)
j = 0
DO WHILE j < lnNumAttach
* Display the attachment filename.
? loImap.GetMailAttachFilename(loEmail,j)
* Display the attachment size in bytes.
lnAttachSize = loImap.GetMailAttachSize(loEmail,j)
? " size = " + STR(lnAttachSize) + " bytes"
j = j + 1
ENDDO
? "--"
i = i + 1
ENDDO
* Disconnect from the IMAP server.
lnSuccess = loImap.Disconnect()
RELEASE loImap
RELEASE loMessageSet
RELEASE loBundle
RELEASE loEmail