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 < 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
Related posts:


hi advance thanks…..
how to delete the message from gmail using java
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);
hi advance thanks…..
how to reply and forward gmail using java
For replying you need to open a SMTP connection separately and do following :
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!