How to Access Gmail Inbox Using Java IMAP

 Posted On  | Yashwant Chavan 

Today we are going to discuss how to read Gmail inbox Using Java IMAP API.Javamail api provides great flexibility to deal with your mail server. Let start with, how to read emails from your inbox box.

Smtp Configuration

Let's create the "smtp.properties" file on local drive and define the smtp configurations like port, hostname, socketfactory class, auth mechanism etc.

mail.smtp.host=smtp.gmail.com
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.auth=true
mail.smtp.port=465

Load Smtp configuration property file

Properties.load(InputStream inStream) method reads all properties from the input byte stream. Once all the Smtp properties are set, use Session.getDefaultInstance() method to create mail session object.

Properties props = new Properties();
props.load(new FileInputStream(new File( "C:\\smtp.properties" )));
Session session = Session.getDefaultInstance(props, null);

Create Store

Create store(javax.mail.Store) to connect , read and retrieve messages from mail server using Internet Message Access Protocol (IMAP)

IMAP stands for Internet Message Access Protocol. It is a method of accessing electronic mail messages stored on a mail server. In other words, it permits a "client" email program to access remote message stores as if they were local. If you want to know more amount IMAP, Please visit IMAP on wiki page

Once you get the store using imaps protocol, You need to connect your mail server with the help of your mail account details

Store store = session.getStore("imaps");
store.connect("smtp.gmail.com", "*************@gmail.com","your_password");

Read Inbox

Define which folder you want to read. Here I used to read Gmail Inbox folder in READ_ONLY mode.

Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount(); 

Read Messages

Read all the messages objects from respected Folder and display each message using for loop.

Message[] messages = inbox.getMessages();
System.out.println("------------------------------");

for (int i = 0; i < 10; i++) {
    System.out.println("Mail Subject:- " + messages[i].getSubject());
}

Full Example

package com.technicalkeeda.app;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

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

public class GmailInbox {

    public static void main(String[] args) {

        GmailInbox gmail = new GmailInbox();
        gmail.read();

    }

    public void read() {

        Properties props = new Properties();

        try {
            props.load(new FileInputStream(new File("C:\\smtp.properties")));
            Session session = Session.getDefaultInstance(props, null);

            Store store = session.getStore("imaps");
            store.connect("smtp.gmail.com", "*************@gmail.com", "your_password");

            Folder inbox = store.getFolder("inbox");
            inbox.open(Folder.READ_ONLY);
            int messageCount = inbox.getMessageCount();

            System.out.println("Total Messages:- " + messageCount);

            Message[] messages = inbox.getMessages();
            System.out.println("------------------------------");

            for (int i = 0; i < 10; i++) {
                System.out.println("Mail Subject:- " + messages[i].getSubject());
            }

            inbox.close(true);
            store.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Output

Total Messages:- 21721
------------------------------
Mail Subject:- Thank You Very Much
Mail Subject:- pls get the pdf
Mail Subject:- hello
Mail Subject:- classpath info
Mail Subject:- Fwd: FW: Interesting
Mail Subject:- Core Java Technologies Tech Tips, May 18, 2005 (Filtering Documents , Java Access Bridge)
Mail Subject:- Core Java Technologies Tech Tips

.....
.....
.....

Mail Subject:- Thnx for ur wishes
Mail Subject:- Core Java Technologies Tech Tips, June 17, 2005 (Beyond J2SE 5.0 ,  Collaborating with Sun on Mustang)
Mail Subject:- about Result


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0334 seconds.