Sample code for 30+ languages & platforms
JavaScript

Get IMAP Attachment Sizes

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailAttachSize method, which returns the size in bytes of an attachment. The first argument is the Email and the second is the zero-based attachment index. This example fetches headers only and prints each attachment's name and size.

Background: Like the attachment filename, the size comes from ckx-imap-* metadata and is available from a header-only fetch without downloading the body. A client can therefore show attachment sizes in a message preview, and decide whether to auto-download or prompt before pulling a large file.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

//  Demonstrates the Imap.GetMailAttachSize method, which returns the size in bytes of an
//  attachment.  The 1st argument is the Email and the 2nd is the zero-based attachment index.

var imap = new CkImap();

imap.Ssl = true;
imap.Port = 993;

success = imap.Connect("imap.example.com");
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

success = imap.Login("user@example.com","myPassword");
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

success = imap.SelectMailbox("Inbox");
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

//  Fetch only the message headers.  Attachment bodies are NOT downloaded, but the ckx-imap-*
//  metadata describing the attachments is present, so the attachment info methods still work.
var headersOnly = true;
var useUid = false;
var seqNum = 1;
var email = new CkEmail();
success = imap.FetchEmail(headersOnly,seqNum,useUid,email);
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

var numAttach = imap.GetMailNumAttach(email);
var i;
for (i = 0; i <= numAttach - 1; i++) {
    var fname = imap.GetMailAttachFilename(email,i);
    if (imap.LastMethodSuccess == false) {
        console.log(imap.LastErrorText);
        return;
    }

    var sz = imap.GetMailAttachSize(email,i);
    console.log(fname + ": " + sz + " bytes");
}

success = imap.Disconnect();
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}