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

Scan for Emails with Attachments and Save Attachments to Files

Scan for emails with attachments and save attachments.

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 requires 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("myLogin", "myPassword");

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

        // We can choose to fetch UIDs or sequence numbers.
        boolean fetchUids = true;

        // Get the message IDs of all the emails in the mailbox
        MessageSet messageSet = new MessageSet();
        try {
            imap.queryMbx("ALL", fetchUids, messageSet);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Fetch the email headers into a bundle object:
        EmailBundle bundle = new EmailBundle();
        boolean headersOnly = true;
        try {
            imap.fetchMsgSet(headersOnly, messageSet, bundle);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Scan for emails with attachments, and save the attachments
        // to a sub-directory.
        Email fullEmail = new Email();
        Email emailHeader = new Email();
        int i = 0;
        while (i < bundle.getMessageCount()) {
            // The bundle contains email headers..
            bundle.emailAt(i, emailHeader);

            // Does this email have attachments?
            // Use GetMailNumAttach because the attachments
            // are not actually in the email object because
            // we only downloaded headers.
            int numAttach = imap.getMailNumAttach(emailHeader);

            if (numAttach > 0) {
                // Download the entire email and save the
                // attachments. (Remember, we
                // need to download the entire email because
                // only the headers were previously downloaded.

                // The ckx-imap-uid header field is added when
                // headers are downloaded.  This makes it possible
                // to get the UID from the email object.
                String uidStr = emailHeader.getHeaderField("ckx-imap-uid");
                int uid = Integer.parseInt(uidStr);

                try {
                    imap.fetchEmail(false, uid, true, fullEmail);
                } catch (ChilkatException e) {
                    System.out.println(e.getLastErrorText());
                    return;
                }

                fullEmail.saveAllAttachments("attachmentsDir");

                for (int j = 0; j < numAttach; j++) {
                    String filename = imap.getMailAttachFilename(emailHeader, j);
                    System.out.println(filename);
                }
            }

            i = i + 1;
        }

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