Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Imap.GetMailSize vs Email.Size

Shows how to get the total size of an email, as well as the sizes of the attachments. This can be done when either full-emails or headers-only are downloaded.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.EmailBundle;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.MessageSet;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Imap imap = new Imap();

        // Connect to an IMAP server.
        // Use TLS
        imap.setSsl(true);
        imap.setPort(993);
        try {
            imap.connect("imap.example.com");

            // Login
            imap.login("****", "****");

            // Select an IMAP mailbox
            imap.selectMailbox("Inbox");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Get the message IDs of all the emails in the mailbox
        // We can choose to fetch UIDs or sequence numbers.
        boolean fetchUids = true;
        MessageSet messageSet = new MessageSet();
        try {
            imap.queryMbx("ALL", fetchUids, messageSet);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // When downloading headers, each email object contains
        // (obviously) the headers, but the body will be missing.
        // Attachments will not be included.  However, it is
        // possible to get information about the attachments
        // as well as the complete size of the email.
        EmailBundle bundle = new EmailBundle();
        boolean headersOnly = true;
        try {
            imap.fetchMsgSet(headersOnly, messageSet, bundle);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Loop over the email objects and display information about each:
        Email email = new Email();
        int i = 0;
        int j = 0;
        int numEmails = bundle.getMessageCount();
        while (i < numEmails) {
            bundle.emailAt(i, email);

            // Display the From and Subject
            System.out.println(email.getFrom());
            System.out.println(email.getSubject());

            // Display the recipients:
            j = 0;
            while (j < email.getNumTo()) {
                System.out.println("TO: " + email.getToName(j) + ", " + email.getToAddr(j));
                j = j + 1;
            }

            j = 0;
            while (j < email.getNumCC()) {
                System.out.println("CC: " + email.getCcName(j) + ", " + email.getCcAddr(j));
                j = j + 1;
            }

            // Show the total size of the email, including body and attachments:
            System.out.println(email.getSize());

            // When a full email is downloaded, we would use the
            // email.NumAttachments property in conjunction with the
            // email.GetMailAttach* methods.
            // However, when an email object contains only the header,
            // we need to access the attachment info differently:
            int numAttach = imap.getMailNumAttach(email);
            j = 0;
            while (j < numAttach) {
                System.out.println(imap.getMailAttachFilename(email, j));
                int attachSize = imap.getMailAttachSize(email, j);
                System.out.println("    size = " + attachSize + " bytes");
                j = j + 1;
            }

            System.out.println("--");

            i = i + 1;
        }

        // Disconnect from the IMAP server.
        imap.disconnect();
    }
}