Java mail class not instantiated from servlet
[nobr]Hi all,
I have written a Java mail class "SendMail.java" which is instantiated in a servlet. I have given my code snippets below :
Servlet
System.out.println("Test 1...............");
String strCRContent = frameCRHTML(crbean, mapJobAccept);
strCRContent = strMessage +"<br>" + strCRContent;
System.out.println("Test 2...............");
SendMail mail =new SendMail();
System.out.println("Test 3...............");
mail.send();
System.out.println("Test 4...............");
SendMail.java
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
publicclass SendMail
{
publicvoid send()
{
System.out.println("Test A..........");
String to ="test1@yahoo.co.in";
String from ="test1@rediffmail.com";
String host ="172.17.9.119";
String subject ="This is subject";
String body ="This is body";
// create some properties and get the default Session
Properties props =new Properties();
props.put("mail.smtp.host", host);
Session session = Session.getInstance(props,null);
try
{
// create a message
Message msg =new MimeMessage(session);
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(body);
Transport.send(msg);
}
catch (MessagingException mex)
{
System.out.println("\n--Exception handling in SendMail.java");
mex.printStackTrace();
}
}
}
When I try to run the SendMail.java alone, it works fine. But when I try to call the class from a servlet by instantiating, the mail is not sent. I even dont get any error messages in my application server console.
Just to track the flow I gave few PRINT statements (System.out.println("")). The control flows only till the line "Test2..................".
But when I comment the lines after "msg.setFrom(new InternetAddress(from));" (including this line) in the SendMail.java, the control flows till the PRINT statement "Test A......." in the SendMail.java. That means the control enters the java class, but doesnt come back to the servlet.
I have added mail.jar and activation.jar (both are latest versions) in the classpath and restarted my application server. Even then the mail is not sent.
I request you all to help me in solving out this problem.
Thanks in advance.[/nobr]
Thanks a lot Mr.bshannon for your advice.
I caught exception during instantiating the mail class and the exception is as follows:
2006-05-31 17:55:35 StandardWrapperValve[ForwardMailServlet]: Servlet.service() for servlet ForwardMailServlet threw exception
java.lang.NoClassDefFoundError: javax/mail/MessagingException
at com.matrixone.apps.edge.servlets.ForwardMailServlet.forwardCR(ForwardMailServlet.java:160)
at com.matrixone.apps.edge.servlets.ForwardMailServlet.doPost(ForwardMailServlet.java:52)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:688)
at java.lang.Thread.run(Unknown Source)
I have placed the latest mail.jar and activation.jar in the folder where other jars are placed and the classpath for all these jars are set in the environment variables. All the other jars are recognised except javax/mail/MessagingException.
FYI please - my java version is 1.3.1_01
Kindly help me...
Thanks in advance...
> I placed the mail.jar and activation.jar in the
> Tomcat lib folder and now my application is able to
> recognise the jars.
Previously you said:
> I have placed the latest mail.jar and activation.jar in the folder
> where other jars are placed and the classpath for all these jars
> are set in the environment variables. All the other jars are
> recognised except javax/mail/MessagingException.
Where exactly were you placing them before?
I'm sure you already found and read this FAQ entry, right?
http://java.sun.com/products/javamail/FAQ.html#servletconfig