【发布时间】:2014-09-04 13:07:52
【问题描述】:
我正在创建一个 Web 应用程序来向某个收件人发送电子邮件。当我运行这段代码时,我遇到了这样的异常
org.apache.jasper.JasperException: 在第 27 行处理 JSP 页面 /two.jsp 时发生异常
24: props.put("mail.smtp.auth", "true");
25: props.put("mail.smtp.port", "465");
26:
27: Session mailSession = Session.getDefaultInstance(props,
28: new javax.mail.Authenticator() {
29: protected PasswordAuthentication
30: getPasswordAuthentication() {
我该怎么办?这是我完整的jsp代码。
<html>
<body>
<%@ page import="java.util.Properties" %>
<%@ page import="javax.mail.Message" %>
<%@ page import="javax.mail.MessagingException" %>
<%@ page import="javax.mail.PasswordAuthentication" %>
<%@ page import="javax.mail.Session" %>
<%@ page import="javax.mail.Transport" %>
<%@ page import="javax.mail.internet.InternetAddress" %>
<%@ page import="javax.mail.internet.*" %>
<%
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 mailSession = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication
getPasswordAuthentication() {
return new PasswordAuthentication
("senderUsername","senderPassword");
}
});
try {
Message message = new MimeMessage(mailSession );
message.setFrom(new InternetAddress("senderemail@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("recipient@gmail.com"));
message.setSubject("hi");
message.setText("text contrnt" +
"\n\n Test email");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
%>
</body>
</html>
有什么帮助吗? 谢谢。
【问题讨论】:
标签: java jsp email smtp jakarta-mail