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

Preview POP3 messages and download a complete email

See more Email Object Examples

Read a short preview for each of up to five messages into an EmailBundle, print their subjects, and download the complete first entry into a separate Email. Configure a POP3 account that supports UIDL and TOP. All messages stay on the server.

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.EmailBundle;
import com.chilkatsoft.chilkat2.MailMan;

public class ChilkatExample {

    public static void main(String[] args) {
        // Assumes Chilkat is unlocked. See Global Unlock Sample.

        MailMan mail = new MailMan();
        mail.setMailHost("pop.example.com");
        mail.setMailPort(995);
        mail.setPopSsl(true);
        mail.setRequireSslCertVerify(true);
        mail.setPopUsername("MAIL_USER");
        mail.setPopPassword("MAIL_PASSWORD");
        EmailBundle previews = new EmailBundle();
        // Keep the messages and request minimal previews. Range indexes are zero-based and inclusive.
        try {
            mail.fetchRange(true, true, 0, 0, 4, previews);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        if (previews.getMessageCount() == 0) {
            System.out.println("There are no messages to preview.");
            try {
                mail.pop3EndSession();
            } catch (ChilkatException e) {
                System.out.println(e.getLastErrorText());
                return;
            }

            return;
        }

        Email header = new Email();
        int i = 0;
        while (i < previews.getMessageCount()) {
            try {
                previews.emailAt(i, header);
            } catch (ChilkatException e) {
                System.out.println(e.getLastErrorText());
                return;
            }

            System.out.println(header.getUidl() + "  " + header.getSubject());
            i = i + 1;
        }

        // A UI could use its selected row here. FetchFull uses the preview's POP3 identity.
        try {
            previews.emailAt(0, header);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Email email = new Email();
        try {
            mail.fetchFull(header, email);

            mail.pop3EndSession();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Full message: " + email.getSubject() + "  Files: " + email.getNumAttachments());

        try {
            email.saveEml("pop-received.eml");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Saved pop-received.eml.");
    }
}