Sample code for 30+ languages & platforms
CkJava 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 CkJava Downloads

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    CkMailMan mail = new CkMailMan();
    mail.put_MailHost("pop.example.com");
    mail.put_MailPort(995);
    mail.put_PopSsl(true);
    mail.put_RequireSslCertVerify(true);
    mail.put_PopUsername("MAIL_USER");
    mail.put_PopPassword("MAIL_PASSWORD");
    CkEmailBundle previews = new CkEmailBundle();
    //  Keep the messages and request minimal previews. Range indexes are zero-based and inclusive.
    success = mail.FetchRange(true,true,0,0,4,previews);
    if (success != true) {
        System.out.println(mail.lastErrorText());
        return;
        }

    if (previews.get_MessageCount() == 0) {
        System.out.println("There are no messages to preview.");
        success = mail.Pop3EndSession();
        if (success != true) {
            System.out.println(mail.lastErrorText());
            return;
            }

        return;
        }

    CkEmail header = new CkEmail();
    int i = 0;
    while ((i < previews.get_MessageCount())) {
        success = previews.EmailAt(i,header);
        if (success != true) {
            System.out.println(previews.lastErrorText());
            return;
            }

        System.out.println(header.uidl() + "  " + header.subject());
        i = i+1;
        }

    //  A UI could use its selected row here. FetchFull uses the preview's POP3 identity.
    success = previews.EmailAt(0,header);
    if (success != true) {
        System.out.println(previews.lastErrorText());
        return;
        }

    CkEmail email = new CkEmail();
    success = mail.FetchFull(header,email);
    if (success != true) {
        System.out.println(mail.lastErrorText());
        return;
        }

    success = mail.Pop3EndSession();
    if (success != true) {
        System.out.println(mail.lastErrorText());
        return;
        }

    System.out.println("Full message: " + email.subject() + "  Files: " + email.get_NumAttachments());

    success = email.SaveEml("pop-received.eml");
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

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