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

Fetch Single Email by UID or Sequence Number

Assuming the UID is known, download a single email by UID from an IMAP mail server.

Chilkat Java Downloads

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

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

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

        Email email = new Email();

        int uid = 2014;
        boolean isUid = true;

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

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

        // Display the Body property, which is the default body.
        // If an email has an HTML body, the Body property contains
        // the HTML source.  Otherwise it contains the plain-text
        // body.
        System.out.println("---- EMAIL BODY ----");
        System.out.println(email.getBody());
        System.out.println("--------------------");

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

        for (int j = 0; j < email.getNumCC(); j++) {
            System.out.println(email.getCcName(j) + ", " + email.getCcAddr(j));
        }

        // 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);
        System.out.println(numAttach);

        for (int j = 0; j < numAttach; j++) {
            System.out.println(imap.getMailAttachFilename(email, j));
            int attachSize = imap.getMailAttachSize(email, j);
            System.out.println("    size = " + attachSize + " bytes");
        }

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

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