Skip to content


Sample java code to read mails from your gmail account

Posted in Internet, J2EE, Java, Technology.

Sample java code to read mails from your gmail account

import java.io.UnsupportedEncodingException;
import java.io.FileWriter;
import java.security.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import com.sun.mail.pop3.POP3SSLStore;
import java.util.Date;

public class GmailFetch {

public static void main(String args[]) throws Exception {

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String <strong>SSL</strong>_FACTORY = "javax.net.ssl.SSLSocketFactory";

// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.<strong>pop3</strong>.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", "<strong>995</strong>");
props.setProperty("mail.pop3.socketFactory.port", "995");
props.setProperty("mail.pop3.host", "<strong>pop.gmail.com</strong>");
props.setProperty("mail.pop3.<strong>user</strong>", args[0]);
props.setProperty("mail.pop3.passwd", args[1]);
props.setProperty("mail.pop3.ssl", "true");

Session session = Session.getInstance(props,null);
<strong>URLName </strong>urln = new URLName("pop3","pop.gmail.com",995,null,args[0], args[1]);
<strong>Store </strong>store = new <strong>POP3SSLStore</strong>(session, urln);
//session.setDebug(true);

store.connect("pop.gmail.com",args[0],args[1]);
<strong>Folder </strong>folder = store.getDefaultFolder();
folder = folder.getFolder("<strong>INBOX</strong>");
folder.open(Folder.READ_ONLY);

System.out.println("<strong>Message </strong>Count "+folder.getMessageCount());
System.out.println("New Message Count "+folder.getNewMessageCount());
System.out.println("=========================================");

Message[] messages = folder.getMessages();

FetchProfile fp = new <strong>FetchProfile</strong>();
fp.add(FetchProfile.Item.ENVELOPE);
folder.fetch(messages, fp);

for (int i = 0; i &lt; messages.length; i++) { System.out.println("From:"+ <strong>messages</strong>[i].getFrom()); } <strong>folder</strong>.close(true); store.close(); } }

Note from Author :
I maintain my website, write articles and reply comments because of my interest/hobby to share information and knowledge.

If you liked the post, Please
Follow me on twitter: http://twitter.com/pankajbatracom
Join My facebook page: http://www.facebook.com/pankajbatra.blog
Join me on LinkedIn: http://www.linkedin.com/in/batrapankaj
Or subscribe to updates by Email by clicking here

To subscribe for updates, Enter your email address:

Related posts:

  1. Reading Mails from secure exchange server in Java
  2. Executing Javascript in Java code
  3. Facebook Email in response to Google Gmail Buzz
  4. Covariant return types
  5. Number of processors on a machine


5 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. abdul basith m says

    hi advance thanks…..
    how to delete the message from gmail using java

    • Pankaj Batra says

      Be sure to open the folder for read/write access:
      folder.open(Folder.READ_WRITE);

      On any message that needs to be deleted, call setFlag like this:
      message.setFlag(Flags.Flag.DELETED, true);

  2. mab says

    hi advance thanks…..
    how to reply and forward gmail using java

    • Pankaj Batra says

      For replying you need to open a SMTP connection separately and do following :

      MimeMessage reply = (MimeMessage) message.reply(false);
      // Set the from field
      reply.setFrom(message.getFrom()[0]);

      // Create the reply content
      MimeMessage original = (MimeMessage) message;
      StringBuffer buffer = new StringBuffer(“”);
      if (original.isMimeType(“text/plain”)) {
      String content = (String) original.getContent();
      StringReader contentReader = new StringReader(content);
      BufferedReader br = new BufferedReader(contentReader);
      String contentLine;
      while ((contentLine = br.readLine()) != null) {
      buffer.append(“> “);
      buffer.append(contentLine);
      buffer.append(“rn”);
      }
      }
      // Set the content
      reply.setText(buffer.toString());

      // Send the message
      Transport.send(reply);

  3. Daniel Craig says

    Hi there, I was looking around for a while searching for security policy template and I happened upon this site and your post regarding de to read mails from Gmail account, Sample, JavaMail, SSL – From Information to Knowledge, I will definitely this to my security policy template bookmarks!







Some HTML is OK

or, reply to this post via trackback.