我想从我的 jsp 页面发送电子邮件。我正在使用 Apache Tomcat 服务器并在本地主机上运行。我提到了这个链接 http://www.javabeat.net/send-mail-jsp/指导我。我还在 JangoSMTP 上创建了一个帐户,并在下面的代码中提供了用户名和密码。但是,我得到的错误是:
An error occurred at line: 24 in the jsp file: /index2.jsp
Session cannot be resolved to a type
21: props.put("mail.smtp.port", "25");
22:
23: //Get the Session object.
24: Session mailSession = Session.getInstance(props,
25: new javax.mail.Authenticator() {
26: protected PasswordAuthentication getPasswordAuthentication() {
27: return new PasswordAuthentication(username,
这是我的 index.html:-
<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<%
String result;
//Recipient's email ID needs to be mentioned.
String to = "xyz@gmail.com";
// Sender's email ID needs to be mentioned
String from = "abc@gmail.com";
final String username = "username";
final String password = "password";
// Assuming you are sending email through relay.jangosmtp.net
String host = "relay.jangosmtp.net";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
//Get the Session object.
Session mailSession = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,
password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(mailSession);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
message.setSubject("Testing Subject");
// Now set the actual message
message.setText("Hello, this is sample for to check send "
+ "email using JavaMailAPI in JSP ");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
result = "Sent message successfully....";
} catch (MessagingException e) {
e.printStackTrace();
result = "Error: unable to send message....";
}
%>
<html>
<head>
<title>Send Email using JSP</title>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
<p align="center">
<%
out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>
请告诉我错误在哪里!我用谷歌搜索了很多但找不到答案。提前致谢!
最佳答案
您包含 .jar 文件。
$TOMCAT_HOME 垃圾桶 session 库 ... 网络应用程序 test.jsp(可以在WEB-INF以外的任意目录下) 网络信息 库 邮件.jar 激活.jar
关于jsp - session 无法解析为通过 jsp 发送邮件的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23253848/