Stack Exchange
javax.mail.SendFailedException: Sending failed; with nested javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair
<html>
<head>
<title>JSP JavaMail Example</title>
</head>
<body>
    <%@ page import="java.util.*"%>
    <%@ page import="javax.mail.*"%>
    <%@ page import="javax.mail.internet.*"%>
    <%@ page import="javax.activation.*"%>
    <%
        String to = request.getParameter("to");     
        String from = request.getParameter("email");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String subject =request.getParameter("subject");
        String messageText = request.getParameter("message");
        //Get the session object  
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");



         Session session2 = Session.getInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("xyz@gmail.com", "abc2014!");//change accordingly
                //return new PasswordAuthentication("abc@gmail.com", "abc2014");//change accordingly                
            }
        }); 

        //compose message  
        try {
            MimeMessage message = new MimeMessage(session2);
            message.setFrom(new InternetAddress(from,name));//change accordingly  
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));
            message.setSubject(subject +" " +name);
            message.setSender(new InternetAddress(from,name));//change accordingly 
            message.setText(" Name: "+name+"\n Email :"+from+"\n Phone No :"+phone+"\n Message : "+messageText);

            System.out.println("before sent message" + to);

            //send message  
            Transport.send(message);

            System.out.println("message sent successfully" + to);

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

        out.println("Mail was sent to " + to);
    %>

</body>
</html>

Above is my Jsp file for sending mail request to my email id from my website contact Form But When i send mail through my webSite i am getting exception as below. It is working Fine when i am using it in my local Machine But Throws Exception when deployed on server.  Please help to resolve this. Thanks in Advance:
org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [jsp] in context with path [] threw exception [An exception occurred processing JSP page /sendMail.jsp at line 62
59:                     System.out.println("message sent successfully" + to);
60:
61:             } catch (MessagingException e) {
62:                     throw new RuntimeException(e);
63:             }
64:
65:             out.println("Mail was sent to " + to);


Stacktrace:] with root cause
 javax.mail.SendFailedException: Sending failed;
  nested exception is:
        class javax.mail.MessagingException: Exception reading response;
  nested exception is:
        javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair
        at javax.mail.Transport.send0(Transport.java:218)
        at javax.mail.Transport.send(Transport.java:80)
        at org.apache.jsp.sendMail_jsp._jspService(sendMail_jsp.java:154)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
        at org.apache.tomcat.util.net.AprEndpoint$SocketWithOptionsProcessor.run(AprEndpoint.java:2440)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:722)


what version of java are you running on machine and what version on server??

server java version: 1.7.0_111
local version: 1.7

is it an option to upgrade both to java8?

But it is working fine on local with 1.7 version

on different versions of JDK the priority of securty providers seems to be different. You can disable the DH provider from your jdk, edit jre/lib/security/java.security -> jdk.tls.disabledAlgorithms= DH

Another solution would be to use https://www.bouncycastle.org implementation of security providers... Download these jars: bcprov-jdk15on-152.jar and bcprov-ext-jdk15on-152.jar, Copy jars to WEB-INF/lib (or your classpath), Add provider dynamically: import org.bouncycastle.jce.provider.BouncyCastleProvider; .... Security.addProvider(new BouncyCastleProvider());

Thanks .. will Try
