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");
           
            fldr.open(Folder.READ_WRITE);
            
            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);

               readContent(m);

                // Uncomment to set "delete" flag on the message
                //m.setFlag(Flags.Flag.DELETED,true);
            }

            // "true" actually deletes flagged messages from folder
            fldr.close(true);
            store.close();

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) {
          System.out.println(
          	"**This is a String Message**");
          System.out.println((String)o);
      }
      else if (o instanceof Multipart) {
          System.out.print(
          	"**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 " +
                                   m.getContentType());

              Object o2 = b.getContent();
              if (o2 instanceof String) {
                  System.out.println(
                  	"**This is a String BodyPart**");
                  System.out.println((String)o2);
              }
              else if (o2 instanceof Multipart) {
                  System.out.print(
                      "**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) {
                  System.out.println(
                      "**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 = is.read()) != -1) {
              System.out.write(c);
          }
      }
	}
	
}

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

    thanks

  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 Google.com 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

Your email address will not be published.