无法使用JavaMail API编译JSP文件 [英] Cannot compile JSP file with JavaMail API
问题描述
我正在努力寻找一种适用于使用JSP发送电子邮件的代码.
我有一个html表单来收集消息的文本主体,然后代码shoudl拥有其余信息,以减少在向团队发送通知时用户的工作....
JSP代码如下,并且我不断收到错误消息,指出无法将Session解析为类型,Message不能解析为类型,MimeMessage不能解析为类型,等等……
<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<%
// SMTP Authentication settings
String host = "smtp.e-tools.com.ve";
String user = "info@e-tools.com.ve";
String pass = "password";
// E-Mail settings
String to = "shee_mass@hotmail.com";
String from = "info@e-tools.com.ve";
String subject = "E-tools Notification - New Document/Comment to see";
String mesg = request.getParameter("smsInput");
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
// Create message to send
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
// Send message
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, user);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
%>
获取不能解析为类型 JSP编译错误虽然导入看起来还不错,但也仅意味着这些类根本不存在在类路径中.就像在普通Java中的运行时获取NoClassDefFoundError
一样.
/WEB-INF/lib
.
在JSP文件而不是Java类中编写原始Java代码并不有趣.
I am struggling to find a code that works in sending emails using JSP.
I have an html form to gather the text body of the message, and then code shoudl have the rest of the information in order to reduce the user´s work at the time of sending notifications out to the team.....
The JSP code is as follows, and I keep getting error messages saying that Session cannot be resolved to a type, Message cannot be resolved to a type, MimeMessage cannot be resolved to a type, etc......
<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<%
// SMTP Authentication settings
String host = "smtp.e-tools.com.ve";
String user = "info@e-tools.com.ve";
String pass = "password";
// E-Mail settings
String to = "shee_mass@hotmail.com";
String from = "info@e-tools.com.ve";
String subject = "E-tools Notification - New Document/Comment to see";
String mesg = request.getParameter("smsInput");
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
// Create message to send
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
// Send message
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, user);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
%>
Getting cannot be resolved to a type JSP compilation errors while imports are seemingly fine can also just mean that those classes are simply not present in the classpath. It's like as getting a NoClassDefFoundError
during runtime in normal Java.
Fact is, in contrary to real Java EE containers such as Glassfish, a simple servletcontainer such as Tomcat doesn't ship with JavaMail. You need to ensure that you've added it yourself by downloading the JavaMail JARs and dropping them in webapp's /WEB-INF/lib
.
Writing raw Java code in JSP files instead of Java classes is not funny.
这篇关于无法使用JavaMail API编译JSP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!