Monday, November 9, 2015

Code for Java Learners to Read a email at the Gmail.

I wanted my Raspberry PI to read my Emails.  So i needed a Java program to to read the mails. Most of the Java programs  I found on net were not working due to SSL certificate errors. Finally here i came up with a code that's working amazingly..!! Atleast for now..!!

  1. import com.sun.mail.util.MailSSLSocketFactory;
  2. import java.security.GeneralSecurityException;
  3. import java.util.Properties;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;

  6. import javax.mail.Folder;
  7. import javax.mail.Message;
  8. import javax.mail.Session;
  9. import javax.mail.Store;
  10. import javax.mail.internet.InternetAddress;

  11. public class Downloader {

  12.  public static void main(String[] args) {
  13.   Downloader gmail = new Downloader();
  14.      try {
  15.          gmail.read();
  16.      } catch (GeneralSecurityException ex) {
  17.          Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
  18.      }
  19.  }

  20.  public void read() throws GeneralSecurityException {
  21.      MailSSLSocketFactory socketFactory= new MailSSLSocketFactory();
  22. socketFactory.setTrustAllHosts(true);

  23.   Properties propsSSL = new Properties();
  24.   propsSSL.put("mail.smtp.host", "smtp.gmail.com");
  25.   propsSSL.put("mail.smtp.socketFactory.port", "465");
  26.   propsSSL.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  27.   propsSSL.put("mail.smtp.port", "465");
  28.   propsSSL.put("mail.imaps.ssl.socketFactory", socketFactory);
  29.   propsSSL.put("mail.smtps.auth", "true");
  30.      propsSSL.put("mail.smtps.ssl.checkserveridentity", "false");
  31.      propsSSL.put("mail.smtps.ssl.trust", "*");
  32.   try {
  33.    
  34.    Session session = Session.getDefaultInstance(propsSSL, null);

  35.    Store store = session.getStore("imaps");
  36.    store.connect("smtp.gmail.com", "yourmail@gmail.com","password");

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

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

  41.    Message[] messages = inbox.getMessages();
  42.    System.out.println("------------------------------");
  43.    for (int i = 0; i < messageCount; i++) {
  44.       System.out.println("Mail Subject:- " + messages[i].getSubject());   
  45.       System.out.println("Time: "+messages[i].getSentDate());
  46.       System.out.println("From: " +InternetAddress.toString(messages[i].getFrom())+"\n"); 
  47.    }
  48.    inbox.close(true);
  49.    store.close();

  50.   } catch (Exception e) {
  51.    e.printStackTrace();
  52.   }
  53.  }

  54. }
Wait, not over yet. You need to allow the google to allow you to access your mails from a less secure device. ie your program..!! Click Here to allow access.




Output:-



Any issues or suggestions: please comment below..!!!

No comments:

Post a Comment