|
|
(JavaScript) 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.
Note: This example requires Chilkat v11.0.0 or greater.
var success = false;
success = false;
// This example assumes the Chilkat API has already been unlocked.
// See Global Unlock Sample for example code.
var imap = new CkImap();
// 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) {
console.log(imap.LastErrorText);
return;
}
// Authenticate with the IMAP server.
success = imap.Login("****","****");
if (success == false) {
console.log(imap.LastErrorText);
return;
}
// Select the mailbox (folder) to access.
success = imap.SelectMailbox("Inbox");
if (success == false) {
console.log(imap.LastErrorText);
return;
}
// 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.
var fetchUids = true;
var messageSet = new CkMessageSet();
success = imap.QueryMbx("ALL",fetchUids,messageSet);
if (success == false) {
console.log(imap.LastErrorText);
return;
}
// 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.
var bundle = new CkEmailBundle();
var headersOnly = true;
success = imap.FetchMsgSet(headersOnly,messageSet,bundle);
if (success == false) {
console.log(imap.LastErrorText);
return;
}
// Iterate over each email in the bundle and display summary information.
var email = new CkEmail();
var name;
var addr;
var i = 0;
var j = 0;
while (i < bundle.MessageCount) {
bundle.EmailAt(i,email);
// Display the sender and subject line.
console.log(email.From);
console.log(email.Subject);
// Display all "To" recipients.
j = 0;
while (j < email.NumTo) {
console.log("TO: " + email.GetToName(j) + ", " + email.GetToAddr(j));
j = j+1;
}
// Display all "CC" recipients.
j = 0;
while (j < email.NumCC) {
console.log("CC: " + email.GetCcName(j) + ", " + email.GetCcAddr(j));
j = j+1;
}
// Display the total size of the email message, including
// the body and all attachments, even though only headers
// were downloaded.
console.log(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.
var numAttach = imap.GetMailNumAttach(email);
j = 0;
while (j < numAttach) {
// Display the attachment filename.
console.log(imap.GetMailAttachFilename(email,j));
// Display the attachment size in bytes.
var attachSize = imap.GetMailAttachSize(email,j);
console.log(" size = " + attachSize + " bytes");
j = j+1;
}
console.log("--");
i = i+1;
}
// Disconnect from the IMAP server.
success = imap.Disconnect();
|