Tuesday, November 10, 2015

Sending a mail in Java


Got this Error..???

"javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:"

Are you searching for the code to send a Email from java program? OR Are you stuck while sending a mail due to this exception.!!! Dont worry I will help you getting it solved.

Step 1: Disable your antivirus or firewalls until we finish this.


Here is how to disable the Avast Shields.. Similarly search for yours.

Step 2: Importing SSL Certificate into your Computer.

  • Open your Gmail and click on the lock symbol.


  • Now Go to connection and open certificate information.
  • Now Go to Details and Then Copy to file.

  • Then just do next next next then browse for file location, give a name and save.
ok... Now we are done saving the certificate, now we need to install it. copy that certificate into C:\Program Files\Java\jdk1.8.0_25\jre\lib\security this location. if you have installed windows in c drive. After pasting, double click that and run it.


now click on Install Certificate and then just next next and finish. then you are done.

Step 3: Getting jar file from apache.

Click here to download the zip file for the jar files which are going to use in our program.

Just extract and add the commons-email-1.4.jar file to your project folder in netbeans.

Step 4: Writing the program.

  1. import java.util.logging.Level;
  2. import java.util.logging.Logger;
  3. import org.apache.commons.mail.*;
  4. /**
  5.  *
  6.  * @author Bisudw
  7.  */
  8. public class Sender {
  9.     public static void main(String [] args){
  10.         try {
  11.             Email email = new SimpleEmail();
  12.             email.setHostName("smtp.gmail.com");
  13.             email.setSmtpPort(465);
  14.             email.setSSLCheckServerIdentity(true);
  15.             email.setStartTLSRequired(true);
  16.             email.setAuthenticator(new DefaultAuthenticator("yourmail@gmail.com", "password"));
  17.             email.setSSLOnConnect(true);
  18.             email.setFrom("yourmail@gmail.com");
  19.             email.setSubject("Test Mail");
  20.             email.setMsg("This is a test mail.");
  21.             email.addTo("receiver's mail@gmail.com");
  22.             email.send();
  23.             System.out.println("Message Sent Successfully..!!");
  24.         } catch (EmailException ex) {
  25.             //System.out.println("Message Sending Filed...!!!");
  26.             Logger.getLogger(Sender.class.getName()).log(Level.SEVERE, null, ex);
  27.         }
  28.     }
  29. }

just run it... If you get any further errors.. Please comment below.

Thank You.

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.