1

i'm trying to send mail using java through gmail but i m getting some exceptions The Code is follows:

 public static void send_java_mail() throws IOException
{
    System.out.println("\n\n\n\nIn send_java_mail");
    String[] recipents=new String[10];
    recipents[0]=email_id;//email_id;       
    //put your smtp details in the properties file.Following given are for gmail account

    Properties smtp_properties =null;
    smtp_properties = new Properties();
    InputStream iss = new FileInputStream("D:\\Heat\\Web\\smtpconfig.properties");
    smtp_properties.load(iss);


    String e_mailbody="Hi\n\tYour Order has been Placed.It will deliever in 2 Days.\n\n\tYour Name :"+CustomerName+"\n\tYour Email Address :"+email_id+"\n\tYour Shipping Address :"+Addr+"\n\tYour Contact Number:"+Phone+"\n\tYour Total amount to be Pay is 3610.\nThanks.";
    String host = smtp_properties.getProperty("host");
    String mail_transport_protocol = smtp_properties.getProperty("mail.transport.protocol");
    String mail_smtp_auth =smtp_properties.getProperty("mail.smtp.auth");
    String mail_smtp_port = smtp_properties.getProperty("mail.smtp.port");
    String mail_debug =smtp_properties.getProperty("mail.debug");
    String mail_smtp_socketFactory_port =smtp_properties.getProperty("mail.smtp.socketFactory.port");
    String mail_smtp_socketFactory_class =smtp_properties.getProperty("mail.smtp.socketFactory.class");
    String mail_smtp_socketFactory_fallback = smtp_properties.getProperty("mail.smtp.socketFactory.fallback");

    Properties props = System.getProperties();
    System.out.println("mail transport protocol :"+mail_transport_protocol);
    props.setProperty("mail.transport.protocol",mail_transport_protocol);     
    props.setProperty("mail.host", host);  
    props.put("mail.smtp.auth", mail_smtp_auth);  
    props.put("mail.smtp.port", mail_smtp_port);  
    props.put("mail.debug", mail_debug);
    props.setProperty("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", mail_smtp_socketFactory_port);  
    props.put("mail.smtp.socketFactory.class",mail_smtp_socketFactory_class);  
    props.put("mail.smtp.socketFactory.fallback", mail_smtp_socketFactory_fallback);  

    final String from="dhanshreeherballife@gmail.com";
    final String pass="D@ny1996";

    Session session = Session.getDefaultInstance(props,  new javax.mail.Authenticator() {    
        protected PasswordAuthentication getPasswordAuthentication() {    
            return new PasswordAuthentication(from,pass);  
            }    
           });  
    MimeMessage message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[recipents.length];

            toAddress[0] = new InternetAddress(recipents[0]);

            message.addRecipient(Message.RecipientType.TO, toAddress[0]);


            message.setSubject("Order");
        message.setText(e_mailbody);
        Transport transport = session.getTransport("smtp");

        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }               
}

The exception is as follows:

DEBUG SMTP: exception reading response: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
javax.mail.MessagingException: Exception reading response;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1090)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:986)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:197)
    at javax.mail.Service.connect(Service.java:233)
    at javax.mail.Service.connect(Service.java:134)
    at Servlets.Sendmail.send_java_mail(Sendmail.java:92)
    at Servlets.PlaceOrder.doGet(PlaceOrder.java:64)
    at Servlets.PlaceOrder.doPost(PlaceOrder.java:72)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
James Z
  • 12,209
  • 10
  • 24
  • 44
Jaydeep Bobade
  • 1,005
  • 2
  • 16
  • 25
  • your issue is in host property you're using to connect, your host doesn't have a valid ssl certificate, don't think your code needs changing, just verify that you host has a valid ssl, if you want a quick hack for this then disable ssl checks completly but that should not be used in production for security risks – user1 Apr 24 '18 at 12:08
  • i tried that but it is not working. can you suggest me how to add ssl certificate. i m using windows 8.1 and i m running project in eclipse – Jaydeep Bobade Apr 24 '18 at 12:15
  • [this answer](https://stackoverflow.com/a/25953317/2650584) gives you steps to add certificat to java key store – user1 Apr 24 '18 at 12:18
  • This should not be required. Can you post the exact parameters your are using? For me it worked with String host = "smtp.gmail.com"; String mail_transport_protocol = "SMPT"; String mail_smtp_auth = "TLS"; String mail_smtp_port = "465"; – Illedhar Apr 24 '18 at 12:24
  • ok i will post in comment all parameter i m using – Jaydeep Bobade Apr 24 '18 at 12:25
  • host =smtp.gmail.com mail.transport.protocol=smtp mail.host=smtp.gmail.com mail.smtp.auth=true mail.smtp.port=465 mail.debug=true mail.smtp.socketFactory.port=465 mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory mail.smtp.socketFactory.fallback=false – Jaydeep Bobade Apr 24 '18 at 12:36
  • @user1 i installed certificate but it is giving same problem. and also enable 8443 port in server.xml – Jaydeep Bobade Apr 24 '18 at 12:38
  • @JaydeepBobade you're connecting to your own server? if so you should provide an ssl connector and a valid certificate, as long as your certificate is not valid you will get this error. consider free ssl certificates if you do not want to buy one similar to [this](https://ssl.comodo.com/free-ssl-certificate.php) I'm sure you can also find others – user1 Apr 24 '18 at 12:46
  • i am not connecting to my own server i m trying to connect gmail server to send mail using my gmail id – Jaydeep Bobade Apr 24 '18 at 12:55

1 Answers1

0

Start by fixing all these common JavaMail mistakes.

The JavaMail FAQ has more information on cert path errors. If you're getting this error when connecting to Gmail, it's most likely that you have a firewall or anti-virus product that's intercepting the connection and presenting its own certificate instead of the Gmail certificate. You should think carefully about the security implications of allowing that software to monitor your connection before using any of the techniques in the link above to disable the security checks.

You should also check that your JDK installation isn't misconfigured or corrupt. Perhaps your application server is overriding the cacerts file (trust store) that comes with the JDK.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40