0

I am trying to write email authentication feature for my website and I encounter some issues. I got

javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; nested exception is: java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl)

The below code is from my RequestedScoped Managed Bean. This is run on Glassfish 3.1 b25

private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final String SMTP_PORT = "465";
private static final String emailSubjectTxt = "Email Confirmation";
private static final String emailFromAddress = "phamtn8@gmail.com";
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";    

@PostConstruct    
public void init(){
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
}

public void sendEmailConfirmation() throws MessagingException{   
    boolean debug = true; 
    String sendTo = "phamtn8@wfu.edu";
    Properties props = new Properties();
    props.put("mail.smtp.host", SMTP_HOST_NAME);
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.smtp.port", SMTP_PORT);
    props.put("mail.smtp.socketFactory.port", SMTP_PORT);
    props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.put("mail.smtp.socketFactory.fallback", "false");
    //It dies at the next line
    Session session = Session.getInstance(props,
        new javax.mail.Authenticator() {
        @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("myUserName", "myPassword");
            }
        });

    session.setDebug(debug);
    //Set the FROM address
    Message msg = new MimeMessage(session);
    InternetAddress addressFrom = new InternetAddress(emailFromAddress);
    msg.setFrom(addressFrom);
    //Set the TO address
    InternetAddress[] addressTo = new InternetAddress[1];
    addressTo[0] = new InternetAddress(sendTo);
    msg.setRecipients(Message.RecipientType.TO, addressTo);
    //Construct the content of the email confirmation
    String message = "Test Content"
    // Setting the Subject and Content Type
    msg.setSubject(emailSubjectTxt);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
}

This is actually a bug for Glassfish 3.1. Here is the bug report

http://java.net/jira/browse/GLASSFISH-15369

Thang Pham
  • 38,125
  • 75
  • 201
  • 285
  • Are you running this on your own or a 3rd party hosted server? – BalusC Dec 28 '10 at 06:09
  • On my own for now. But eventually will switch t 3rd party hosted server – Thang Pham Dec 28 '10 at 06:21
  • Oh, you changed the exception message, now the question in my comment doesn't make sense anymore. Edit: isn't there more into the trace? The last cause mentions about "an error". – BalusC Dec 28 '10 at 06:26
  • Sorry, I find out that I need to change `Session.getDefaultInstance` to `Session.getInstance`. And that fixed the previous error, but now I seems like I can connection to smtp.gmail.com. What bug me is that, the same code, if I run as a Java application instead of running instead managed bean, then it work. – Thang Pham Dec 28 '10 at 06:34
  • 1
    Google gives [some hits](http://www.google.com/search?q=javax.mail.MessagingException%3A+Could+not+connect+to+SMTP+host%3A+smtp.gmail.com%2C+port%3A+465%3B+nested+exception+is%3A+java.net.SocketException%3A+java.security.NoSuchAlgorithmException%3A+Error+constructing+implementation+%28algorithm%3A+Default%2C+provider%3A+SunJSSE%2C+class%3A+com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl%29) on exactly this exception and many of them are related to Glassfish 3.1. Look like sort of bug. – BalusC Dec 28 '10 at 06:41
  • Now that just great :( ohh god. What other option for send out mail (plain-text is fine, encrypted is prefer), BalusC – Thang Pham Dec 28 '10 at 06:46
  • 1
    Not a direct answer, but when you're using Glassfish Enterprise, you might try using the Glassfish-provided capabilities to configure Javamail as JNDI resource. See also [this manual](http://docs.sun.com/app/docs/doc/820-7695/beaow?l=en&a=view). – BalusC Dec 28 '10 at 06:53
  • Thank you. This definitely look like something I could use here. Thank you – Thang Pham Dec 28 '10 at 07:17
  • 1
    Try using IMAP http://stackoverflow.com/questions/61176/getting-mail-from-gmail-into-java-application-using-imap – Preston Dec 28 '10 at 14:36
  • Thank you. I will definitely take a look at them – Thang Pham Dec 28 '10 at 14:55

1 Answers1

2

I think this is an SSL related issue. Can you try adding the following lines in the <java-config> section of your domain.xml and restart glassfish (note that you should shutdown GF before editing this file).

<jvm-options>-Djavax.net.ssl.keyStorePassword=changeit</jvm-options>
<jvm-options>-Djavax.net.ssl.trustStorePassword=changeit</jvm-options>

where changeit is the default password of your ssl certificate.