开发者

Cannot compile JSP file with JavaMail API

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(n开发者_运维技巧ew 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.


Sheeyla, I had the exact same problem. I am using Tomcat6. The issue is that Tomcat6 doesn't like the mail.jar folder being in the WEB-INF/lib folder and insists on it being a globally shared jar to work. Put the mail.jar in the CATALINA_HOME/lib folder (/usr/share/tomcat6/lib by default), delete it from the WEB-INF/lib folder, restart Tomcat6 and it'll work. You don't need the activation.jar since you're most likely on java 1.6 either so you can get rid of that too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜