I have the following code to send an email from a servlet.Servlet read mail parameters from an
html file and then sends email ,but getting error i.e java.lang.NoClassDefFoundError: javax/mail/Address
Following is the servlet code and Exception its generating :
SEVERE: Allocate exception for servlet mailservlet
java.lang.NoClassDefFoundError: javax/mail/Address
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2328)
at java.lang.Class.getConstructor0(Class.java:2640)
at java.lang.Class.newInstance0(Class.java:321)
at java.lang.Class.newInstance(Class.java:303)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1089)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:791)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:127)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:595)
4-Mar-2009 4:44:45 PM org.apache.catalina.core.StandardWrapperValve invoke
INFO: Servlet mailservlet is currently unavailable
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35223
7
posted
0
You need the JavaMail and Java Activation Framework jar files in your classpath (which, for a servlet, would be the WEB-INF/lib directory). They are named something like mail-1.4.2.jar and
activation-1.1.1.jar.
I am using MyEclipse for this project and these 2 files you recommended are already included in Java build path
in J2EE 1.4 Library Container.when I try to add them as External JAR files it is giving warning that files are already included.
Do you still recommend to add it ?
Nilesh response :
1 ) Your code definetly must be working and a very good reference and start for me to learn about Java Mail API , but I want to give a try to code in my servlet,just wondering is there anything missing or wrong in my code?
2) As I have get this code from some tutorial ,just curious about 2nd argument of Properties i.e
props.put("mail.smtp.host","localhost");
what should be the value in 2nd argument if I don't know about host,as in my case I am getting both email addresses (to and from ) through an
html page!
Thanks.
Kuldeep Tewari
Ranch Hand
Joined: May 22, 2006
Posts: 35
posted
0
Mishaal Khan wrote:
what should be the value in 2nd argument if I don't know about host
You ought to have a SMTP host for sending email.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35223
7
posted
0
Mishaal Khan wrote:TI am using MyEclipse for this project and these 2 files you recommended are already included in Java build path
in J2EE 1.4 Library Container.when I try to add them as External JAR files it is giving warning that files are already included.
Do you still recommend to add it ?
"NoClassDefFoundError" does not occur at build time, so whatever you do with the build path is irrelevant. You need to add those libraries to the classpath used at runtime.
Your code definetly must be working
Why? Changing the code does nothing to resolve a NoClassDefFoundError - it's a classpath issue.
props.put("mail.smtp.host","localhost");
what should be the value in 2nd argument ?
That's the server name of your email server. "localhost" is correct only if you have a mail server running on your machine.
Mishaal Khan
Ranch Hand
Joined: Aug 21, 2008
Posts: 61
posted
0
Ulf,
I added the jars in lib files and that particular problem is solved.Thanks. But have another problem.
I assume from your reply that 2nd argument for Properties should be host email server,so if I am expecting
a mail on hotmail account it should be "smtp.hotmail.com" ? Code that Nilesh posted also has 2nd
argument as "smtp.gmail.com".
I don't know if HotMail's server can be used like this, and if it can, whether that's the correct address to use. You should use the mail server of your ISP (or your company's mail server); you can find its host name or address in the settings of your email client.
nilesh beit
Greenhorn
Joined: Feb 04, 2009
Posts: 13
posted
0
Hey
I found two links which indicate that it may not possible with smtp.hotmail.com
Try sending with gmail first just to check whether setup is rigtht. Check you have setup everything as Ulf suggested. And then find ways for hotmail
Regards
Mishaal Khan
Ranch Hand
Joined: Aug 21, 2008
Posts: 61
posted
0
Nilesh,
I change my code according to your code with only difference as I have defined SMTPAuthenticator class not as inner class but as seperate class.I have
hardcoded the userid and password inside the class.
And also instead of
Authenticator auth = new SMTPAuthenticator() , I have instantiate this class as:
SMTPAuthenticator auth = new SMTPAuthenticator(), because on defining Authenticator auth = new SMTPAuthenticator(), its giving error 'type Authenticator is ambiguos'
so I instantiate it as SMTPAuthenticator auth = new SMTPAuthenticator();
but getting this exception :
ERROR.....javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.
You can go back and change your post to add code tags by clicking the button on your post.
Host name is working fine with another example above (Nilesh Biet) code.
I did not specify any port no in my code,it is giving port no in exception message!
Thanks.
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted
0
I was just translating the exception message into easy-to-understand words for newbies. It's up to you to find out the root cause of the problem. Which thus can be a wrong hostname/port or a blocking firewall/router.
nilesh beit
Greenhorn
Joined: Feb 04, 2009
Posts: 13
posted
0
SMTPAuthenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
This might be causing problem
You said you have created new class SMTPAuthenticator , but have you extended it from javax.mail.Authenticator . That is required.
This line should be
The class Authenticator represents an object that knows how to obtain authentication for a network connection.
Applications use this class by creating a subclass, and registering an instance of that subclass with the session when it is created.
When authentication is required, the system will invoke a method on the subclass (like getPasswordAuthentication)
Mishaal Khan
Ranch Hand
Joined: Aug 21, 2008
Posts: 61
posted
0
Thanks for the reply.
When I dedfined
Authenticator auth = new SMTPAuthenticator(), its giving error 'type Authenticator is ambiguos'
then I changed it to SMTPAuthenticator auth = new SMTPAuthenticator(),
and the error was gone,but the application is not working as required. You have any idea why it is giving
this error 'type Authenticator is ambiguos' ?And Yes I have extended javax.mail.Authenticator in my class.
Thanks.
Mishaal Khan
Ranch Hand
Joined: Aug 21, 2008
Posts: 61
posted
0
OK, the error 'type Authenticator is ambiguos ' is gone when I imported
javax.mail.Authenticator as a seperate statement , although I already had import javax.mail.* statement in my servlet.
so this problem is solved.But still getting exception
ERROR.....javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted
0
When I dedfined
Authenticator auth = new SMTPAuthenticator(), its giving error 'type Authenticator is ambiguos'
You have more than one class with the name "Authenticator" in the same classpath scope (imported packages and the current package). The compiler can't determine based on the coding which one to use and is thus giving this error. This has nothing to do with your initial problem however.
The answer on the hostname/port number is to be found in the developer documentation of the mail service in question. Go to gmail.com and check it there.
Regarding the firewall/router, you need to check the whole network path if there isn't any software or hardware which acts as a firewall/router and if it is properly configured. It should allow the access to/from the specified hostname/port.
Mishaal Khan
Ranch Hand
Joined: Aug 21, 2008
Posts: 61
posted
0
Thanks for the replies.
Actually I am not very knowledgeable in networking side ,so don't know how to check or configure the
network path . I have ZoneAlarm installed on my system and I guess its creating this problem.
Would anyone please guide me in checking or configuring settings of firewall or router.
Thanks.
nilesh beit
Greenhorn
Joined: Feb 04, 2009
Posts: 13
posted
0
Hello
Thats not a zonealarm problem.
Assuming you are directly connecting to internet through broadband\modem and not a firewalled router which is blocking smtp and pop ports.
Add some properties shown below
Mishaal Khan
Ranch Hand
Joined: Aug 21, 2008
Posts: 61
posted
0
Thaaaaaaank you very much Nilesh.It worked and mail has been sent.
I tried the same example with yahoo.com as its smtp address is smtp.mail.yahoo.com and port is 25 ,but my email address is yahoo.ca
so again having exception Connection time out on port 25.Does it make any difference yahoo.com or yahoo.ca?
Would you please guide how can we get the particular smpt host name ?
You get the SMTP host name by asking the owner of the SMTP host. Or by finding online instructions posted by the owner of the host.
If your email address is "something@yahoo.ca" then you will want to use the SMTP host for yahoo.ca to send your e-mails. If you use some other host, then quite likely it will refuse to send your messages. Or if it doesn't, then the recipient's host will quite likely notice that you are using a loose server and will flag them as probable spam. And be prepared to authenticate yourself to that host. If yahoo.ca allows direct access to its SMTP server, you should find instructions on how to do that somewhere on the yahoo.ca site.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Getting exception in sending Email from Servlet