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]

[3959 byte] By [avinash_gurua] at [2007-10-2 20:32:48]
# 1

Obviously you're getting an exception that your code is not catching,

probably some sort of RuntimeException, most likely due to some

classes not being found.

Wrap the instantiation of the SendMail object in a try/catch block

that catches Exception and print out the exception. Most likely you

made some mistake when adding mail.jar and activation.jar to your

CLASSPATH.

bshannona at 2007-7-13 23:15:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

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...

avinash_gurua at 2007-7-13 23:15:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
There's obviously something wrong with your configuration but I can'ttell what. Your server isn't finding the JavaMail classes, possibly becauseit's not finding mail.jar.Did you restart your server after installing mail.jar?
bshannona at 2007-7-13 23:15:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Seems to me that you shouldn't have to install any JavaMail jars for Tomcat to run, it should already have them somewhere. And if you start mucking about installing them, either in the Tomcat internal locations or in your own web application, you're likely to run into version problems or classloader problems or both.

DrClapa at 2007-7-13 23:15:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
Thanks a lot bshannon and DrClap for your timely help.I placed the mail.jar and activation.jar in the Tomcat lib folder and now my application is able to recognise the jars.Its working fine now.
avinash_gurua at 2007-7-13 23:15:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
Thanks a lot bshannon and DrClap for your timely help.I placed the mail.jar and activation.jar in the Tomcat lib folder and now my application is able to recognise the jars.Its working fine now.
avinash_gurua at 2007-7-13 23:15:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7

> 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

bshannona at 2007-7-13 23:15:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8

Earlier i had placed the jars outside Tomcat and set them in the classpath. (As our application is built on eMatrix, an eBusines apllication we usually place our application related jars in the lib folder of eMatrix)

Later when i placed them in the Tomcat lib (C:\Tomcat5.0\webapps\EDGECOL\WEB-INF\lib) the system recognised the jars.

avinash_gurua at 2007-7-13 23:15:58 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...