Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

Receive POP3 Emails using Java

Posted on 17/12/2009

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 thoughts on “Receive POP3 Emails using Java”

  1. Glen says:
    05/05/2011 at 07:36

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

    thanks

    Reply
  2. John Turcott says:
    17/01/2014 at 18:08

    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

    Reply
  3. mwesigye says:
    25/03/2014 at 16:54

    this is so cool. thanks for you time

    Reply

Leave a Reply to Glen Cancel reply

Your email address will not be published. Required fields are marked *


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
d8252545e156cc094d04b47597b4fad1-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme