我正在尝试为我的网站编写电子邮件身份验证功能,但我遇到了一些问题。我得到了
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)
以下代码来自我的RequestedScoped
受管Bean。这是在 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);
}
这实际上是Glassfish 3.1的一个错误。这是错误报告
答案 0 :(得分:2)
我认为这是一个与SSL相关的问题。您可以尝试在domain.xml的<java-config>
部分添加以下行并重新启动glassfish(请注意,在编辑此文件之前应关闭GF)。
<jvm-options>-Djavax.net.ssl.keyStorePassword=changeit</jvm-options>
<jvm-options>-Djavax.net.ssl.trustStorePassword=changeit</jvm-options>
其中 changeit 是您的ssl证书的默认密码。