I’ve taken over some old legacy JSP code and wanted to add some mail support to it. Well, it didn’t go so well, as I get errors, such as:
Session cannot be resolved to a type
An error occurred at line: 39 in the jsp file: /xxxxx/test.jsp
Message cannot be resolved to a type
MimeMessage cannot be resolved to a type
etc
the code, sample taken off somewhere (not pretty…):
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%@ page session="true"%>
<html>
<head>
<title>JSP JavaMail Example </title>
</head>
<body>
<%
String to = "sssssss@gmail.com";
String from = "admin@yyyyyy.com";
String subject = "subject";
String messageText = "body";
Properties props = System.getProperties();
//props.put("mail.host", host);
//props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);
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);
Transport.send(msg);
%>
</body>
</html>
Googling indicated the actual mail jars may be missing, so I download javamail, extracted the jars and put them into the tomcat WEB-INF directory. Without a change.
This a rackhostcloud setup with CentOs (5.5?) on it. This was configured by the old team (long gone). Safe to say, I’m not going to be known as the best Linux admin around, but basic command line stuff is no problem. I can run yum just fine, but I think there is more to it then this at this point. Perhaps some Java config?
I just want to send a simple mail…
Any suggestions, much appreciated.
The mail jars need to go in WEB-INF/lib, not in WEB-INF. You’ll need to restart the app or the entire tomcat server for it to take effect.
From your description, I’m guessing you’re working with an exploded webapp directory found at $TOMCAT_HOME/webapps/. First thing to be aware of: if there’s a .war right next to the directory, then the war is the “source of truth”. A war is a Web ARchive. It’s basically a zip file containing an entire webapp directory, and Tomcat extracts that directory for performance purposes. What that means to you is that whatever you do to the directory, if that war file’s timestamp gets updated somehow, it will blow away all changes you’ve made to the directory and replace it with the contents of the war again.
Now after that, all you should need in order to use the mail classes is a file structure like: