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.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(); } }

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. Java Interview questions
  • abdul basith m

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

    • http://www.pankajbatra.com Pankaj Batra

      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);

  • mab

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

    • http://www.pankajbatra.com 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
      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);

  • 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!