Receive POP3 Emails using Java

Take a look at this example on how to read pop3 emails.
But there are 3 parameters you need to prepare: your pop3 server, username and passowrd for that server.

        String host = "<your pop3 server>";
        String user = "<your user name>";
        String password = "<your password>";
        String subjectSubstringToSearch = "Test E-Mail through Java";

        Session session = Session.getInstance(new Properties());

        try {
            // Get a Store object
            Store store = session.getStore("pop3");
            store.connect(host, user, password);
            // Get "INBOX" folder
            Folder fldr = store.getFolder("INBOX");
            int count = fldr.getMessageCount();
            System.out.println(count  + " total messages");

            // Message numbers start at 1
            for(int i = 1; i <= count; i++) {
                // Get  a message by its sequence number
                Message m = fldr.getMessage(i);

                // Get some headers
                Date date = m.getSentDate();
                Address [] from = m.getFrom();
                String subj = m.getSubject();
                String mimeType = m.getContentType();
                System.out.println(date + "\t" + from[0] + "\t" +
                                    subj + "\t" + mimeType);


                // Uncomment to set "delete" flag on the message

            // "true" actually deletes flagged messages from folder

Pretty straight forward, not much to explain.
All that is missing is the readContent() function which prints the message.getContent().

First you need to understand that an Email message content can be just a regular String or an input stream, but in most cases it is a Multipart object which can be composed of different parts of different types. It can also actually contain inner multiparts.

Take a look at readContent()

public static void readContent(Message m)
throws IOException, MessagingException{
    Object o = m.getContent();
    if (o instanceof String) {
          	"**This is a String Message**");
      else if (o instanceof Multipart) {
          	"**This is a Multipart Message.  ");
          Multipart mp = (Multipart)o;
          int count3 = mp.getCount();
          System.out.println("It has " + count3 +
              " BodyParts in it**");
          for (int j = 0; j < count3; j++) {
              // Part are numbered starting at 0
              BodyPart b = mp.getBodyPart(j);
              String mimeType2 = b.getContentType();
              System.out.println( "BodyPart " +
              										 (j + 1) +
                                  " is of MimeType " +

              Object o2 = b.getContent();
              if (o2 instanceof String) {
                  	"**This is a String BodyPart**");
              else if (o2 instanceof Multipart) {
                      "**This BodyPart is a nested Multipart.");
                  Multipart mp2 = (Multipart)o2;
                  int count2 = mp2.getCount();
                  System.out.println("It has " + count2 +
                      "further BodyParts in it**");
              else if (o2 instanceof InputStream) {
                      "**This is an InputStream BodyPart**");
          } //End of for
      else if (o instanceof InputStream) {
          System.out.println("**This is an InputStream message**");
          InputStream is = (InputStream)o;
          // Assumes character content (not binary images)
          int c;
          while ((c = != -1) {

You will also need the mail.jar that can be found here.

downloaddownload source

3 Replies to “Receive POP3 Emails using Java”

  1. best, clearest answer on the internet
    I easily converted it to Javascript and it worked the first time


  2. Your code looks streight forward, however I can’t seem to get it to run even after changing it to javascript. Basically I need a script to verify email address actual exist before sending emails to non-existing accounts. Do you have any idea of where I can location something like this. I’ve tried looking through good old for the past week, but there doesn’t seem to be any code out there in Javascript or ASP.
    I send using both using ASP and Javascript without a problem. But have no idea on how to receive or verify an account exist.
    Thank you for your time, John Turcott

Leave a Reply to John Turcott Cancel reply

Your email address will not be published.