Sample java code to read mails from your gmail account

Monday, 23 April 2007, 23:13 | Category : Internet, J2EE, Java, Technology
Tags : ,

Sample java code to read mails from your gmail account

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 {

final String <strong>SSL</strong>_FACTORY = "";

// 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("", "<strong></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","",995,null,args[0], args[1]);
<strong>Store </strong>store = new <strong>POP3SSLStore</strong>(session, urln);

<strong>Folder </strong>folder = store.getDefaultFolder();
folder = folder.getFolder("<strong>INBOX</strong>");;

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

Message[] messages = folder.getMessages();

FetchProfile fp = new <strong>FetchProfile</strong>();
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(); } }
  • abdul basith m

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

    • Pankaj Batra

      Be sure to open the folder for read/write access:;

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

  • mab

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

    • Pankaj Batra

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

      MimeMessage reply = (MimeMessage) message.reply(false);
      // Set the from field

      // 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(“> “);
      // Set the content

      // Send the message

  • Daniel Craig

    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!