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

Download MIME Source of Emails in IMAP Mailbox

Demonstrates how to download the MIME source for emails on an IMAP server.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.StringBuilder;

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

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

        // The NumMessages property contains the number of messages in the selected mailbox.
        int numMessages = imap.getNumMessages();
        if (numMessages == 0) {
            System.out.println("No messages exist in the Inbox.");
            return;
        }

        StringBuilder sbMime = new StringBuilder();
        for (int seqNum = 1; seqNum <= numMessages; seqNum++) {
            sbMime.clear();
            try {
                imap.fetchSingleAsMimeSb(seqNum, false, sbMime);
            } catch (ChilkatException e) {
                System.out.println(e.getLastErrorText());
                return;
            }

            // The MIME source of the downloaded email is contained in sbMime.
        }

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