JavaMail – read email

Today we have a quick update for your, code that can be used to read email messages from IMAP or POP3 server. This code uses JavaMail API for reading emails from the server. It displays inbox contents including email sender, title and body. Take a look at our code.

JavaMail read emails- the code


import java.util.Properties;

import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

* Class reads emails
* @author itcuties
public class JavaMailReader {

   public static void main(String[] args) {

    * Method reads emails from the IMAP or POP3 server.
    * @param isImap - if true then we are reading messages from the IMAP server, if no then read from the POP3 server.
   private static void readEmails(boolean isImap) {
       // Create all the needed properties - empty!
       Properties connectionProperties = new Properties();
       // Create the session
       Session session = Session.getDefaultInstance(connectionProperties,null);
       try {
           System.out.print("Connecting to the IMAP server...");
           // Connecting to the server
           // Set the store depending on the parameter flag value
           String storeName = isImap ? "imaps" : "pop3";    
           Store store = session.getStore(storeName);
           // Set the server depending on the parameter flag value
           String server = isImap ? "" : "";
           // Get the Inbox folder
           Folder inbox = store.getFolder("Inbox");
           // Set the mode to the read-only mode
           // Get messages
           Message messages[] = inbox.getMessages();
           System.out.println("Reading messages...");
           // Display the messages
           for(Message message:messages) {
               for (Address a: message.getFrom())
                   System.out.println("From:" + a);
               System.out.println("Title: " + message.getSubject());
       } catch (Exception e) {


When you run this code you will get the following output:

Connecting to the IMAP server...done!
Reading messages...
From:Team ITC <>
Title: Test message subject

Hello email :)

From:Team ITC <>
Title: Message title

Hi there!
Take IT easy



If you are using maven like we do, this is the pom.xml file with all the needed artifacts the javamail, javax.activation and the javax.mail extensions.

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
    <!-- JavaMail API -->  
  	<!-- Java Activation -->
  	<!-- JavaMail extension -->

Download this sample code here.

This code is available on our GitHub repository as well.

8 Responses to "JavaMail – read email"

  1. Meghna says:

    Nice Example.. thanks was very helpful for my project…

  2. ami says:

    I have exception PKIX path building failed: unable to find valid certification path to requested target;

    My server doesn’t have certificate.
    please help me.

  3. Chandu says:

    Hi, I used above code to read emails from my gmail account but I am getting null pointer exception at message.getContent();

    Can u please tell me y i am getting that exception.

    I added mail.jar and activation.jar in my classpath and I am getting remaining details from gmail except content.

    Thank you.

  4. jalaja says:

    how to read mails using java other than imap and pop3 protocols??

    In IT companies they are not giving any exchange server names.

    can you please suggest any other way..

  5. Emmanuel Anibaba says:

    God Bless you, i couldn’t believe it, it works like magic

  6. Kirti Sapra says:

    Thanks! It worked for me!

  7. naresh says:


    javax.mail.AuthenticationFailedException: Login incorrect
    	at com.sun.mail.imap.IMAPStore.protocolConnect(
    	at javax.mail.Service.connect(
    	at javax.mail.Service.connect(
    	at com.naresh.ReadMail.readEmails(
    	at com.naresh.ReadMail.main(

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>