Java 如何从JSP发送电子邮件?我有个错误
我试图使用gmail从JSP发送电子邮件,我在一个普通的Java应用程序(不是web)中测试了我的代码,它运行良好,但我遇到了这个问题Java 如何从JSP发送电子邮件?我有个错误,java,jsp,Java,Jsp,我试图使用gmail从JSP发送电子邮件,我在一个普通的Java应用程序(不是web)中测试了我的代码,它运行良好,但我遇到了这个问题 java.lang.NoSuchMethodError:sun.security.ssl.Handshaker.setApplicationProtocols([Ljava/lang/String;)V 当我运行JSP时,下面是我的代码 <% String result = "failed"; Properti
java.lang.NoSuchMethodError:sun.security.ssl.Handshaker.setApplicationProtocols([Ljava/lang/String;)V
当我运行JSP时,下面是我的代码
<%
String result = "failed";
Properties props = new Properties();
String gmailAccount = "mymail@gmail.com";
String gmailPassword = "mypass";
String body = "<h1>Hello!</h1>";
String subject = "Test";
String to = "destinationmail@gmail.com";
Session sessionmail;
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");
sessionmail = Session.getInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(gmailAccount, gmailPassword);
}
});
try {
MimeBodyPart a = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(body, "text/html");
multipart.addBodyPart(mimeBodyPart);
Message message = new MimeMessage(sessionmail);
message.setFrom(new InternetAddress(gmailAccount));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setContent(multipart);
Transport transport = sessionmail.getTransport("smtp");
transport.connect("smtp.gmail.com", gmailAccount, gmailPassword);
transport.sendMessage(message, message.getAllRecipients());
result = "Ok";
} catch (MessagingException e) {
result = "failed";
}
%>
顺便说一句,我正在使用JDK1.8.0û251和Payara服务器5.194(我尝试使用glassfish,但我得到了相同的错误)如果您的代码在控制台应用程序中工作,但在Payara和glassfish中引发了该异常,那么很可能是服务器加载包含该类不同版本的JAR文件导致的问题 提到了
grizzly npn bootstrap.jar
。请参阅第三个错误。查找显示了提到问题相同原因的其他结果
在服务器安装中查找该文件,并将其重命名为grizzly npn bootstrap.old
(或尝试服务器的较新版本),然后重试