0

I have used javamail in several projects before and it has always functioned well, until recently, (Maybe something to do with upgrading to java 8?) when it now always returns the following exception:

Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  nested exception is:
    javax.net.ssl.SSLKeyException: RSA premaster secret error
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1963)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:367)
    at javax.mail.Service.connect(Service.java:226)
    at javax.mail.Service.connect(Service.java:175)
    at javax.mail.Transport.send0(Transport.java:253)
    at javax.mail.Transport.send(Transport.java:124)
    at one.Demo.main(Demo.java:39)
Caused by: javax.net.ssl.SSLKeyException: RSA premaster secret error
    at sun.security.ssl.RSAClientKeyExchange.<init>(Unknown Source)
    at sun.security.ssl.ClientHandshaker.serverHelloDone(Unknown Source)
    at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
    at sun.security.ssl.Handshaker.processLoop(Unknown Source)
    at sun.security.ssl.Handshaker.process_record(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:528)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:333)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 7 more
Caused by: java.security.NoSuchAlgorithmException: SunTlsRsaPremasterSecret KeyGenerator not available
    at javax.crypto.KeyGenerator.<init>(KeyGenerator.java:159)
    at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:208)
    at sun.security.ssl.JsseJce.getKeyGenerator(Unknown Source)
    ... 20 more

From what I make of it, it seems the error is derived from the send method of the transport class. Here is an MCVE of the problem I'm having. It uses Gmail stmp servers.

import java.util.Date;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;

public class Demo {

  public static void main(String[] args) throws MessagingException {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", 465);
    props.put("mail.smtp.ssl.enable", true);
    props.put("mail.smtp.auth", true);
    Authenticator auth = new Authenticator() {
      private PasswordAuthentication pa = new PasswordAuthentication(
        "my_email",
        "my_password");
      public PasswordAuthentication getPasswordAuthentication(){
        return pa;
      }
    };
    Session session = Session.getInstance(props, auth);
    session.setDebug(false);

    MimeMessage message = new MimeMessage(session);

    Address address = new InternetAddress("other_email");
    message.addRecipient(Message.RecipientType.TO, address);

    message.setFrom(new InternetAddress("my_email"));
    message.setSubject("Test");
    message.setText("This is a test of the Javamail API.");
    message.setSentDate(new Date());

    Transport.send(message);
  }

}
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
Dane Johnson
  • 314
  • 3
  • 11
  • maybe this helps: http://stackoverflow.com/questions/2872520/suntlsrsapremastersecret-keygenerator-not-available – salyh May 19 '14 at 09:14

1 Answers1

0

See if this post helps. It looks like some problem related to the installation or configuration of your JDK. Also, are you going through a proxy server or firewall?

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