Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with a JavaMail problem  RSS feed

 
Sougata Som
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I need some help with a JavaMail API problem.
I'm working on a J2EE app, which can be configured to send mails on certain events. There's a page where the user needs to specify the SMTP server and optionally the username and password if the SMTP server requires authentication. Now, the requirement is that when the user enters this information and clicks on Save, the system should validate whether the values entered are valid or not and display an error message in case of invalid data.
I created a standalone program first which works fine. However, when I put that code in the application, i get an exception.
Here's the standalone program:



Now, the method i'm using in my application is exactly the same as the go() method above except how the messages get displayed to the user.
But i'm always gettting MessagingException, even when I've entered the correct values for SMTP server, user and password.
Here's the error message i'm getting:

2009-12-17 16:41:16,112 DEBUG [com.inmedius.mentor.ui.browser.ServerBean] [admin - ServerConfigurationController]: javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketException: Connection reset
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1925)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1684)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at com.inmedius.mentor.ui.browser.controller.ServerConfigurationController.validateSmtpConnection(ServerConfigurationController.java:715)
............
............


Could anyone please guide me regarding where the problem could be? I'm pretty inexperienced with JavaMail. I have gone through the JavaMail FAQ but didn't find anything that could help me in this case. If I missed something, please point it out. Thanks a lot.
 
Narendra shah
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sougata Its look like that your server ip address is not reachable to your application host.

Can you verify that is it your server host is pinged from your application machine ?
 
Sougata Som
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I'm able to ping the server host. In fact, the standalone program that I've copied above, is able to connect to the server if the data entered is valid and throws the correct exceptions when invalid data is entered. However, when I copied the code from the go() method of the standalone program to a class in my application, things don't work anymore and I always get the same MessagingException when transport.connect() is called.

I'm using 2 SMTP servers for testing - one of them doesn't require authentication and the other one does. I tried connecting to the SMTP server which doesn't require authentication from my app using java.net.Socket and it worked. But the requirement is that the code should work for servers with authentication also. I don't know if that's possible using Socket.

Any help in solving the problem is appreciated, whether using JavaMail or not. Thanks.
 
Narendra shah
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
# transport = mailSession.getTransport();

Change above line as passing argument withing

# transport = mailSession.getTransport("smtp");

Check Mail api session class api for more info.



 
Sougata Som
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Narendra,
I made the change suggested by you but it's still throwing the same exception at the same line.
 
Narendra shah
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sougata Som wrote:Hi Narendra,
I made the change suggested by you but it's still throwing the same exception at the same line.


Can you send stack trace of your exception with message. And log all parameters which you are passing to your program. Above given code is completely valid. And by using that mail should be sent.

 
Sougata Som
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm on leave now, so I'll get back to you with the stack trace when I'm back in office again. But just a thought - could this have something to do with multi-threading? Am I supposed to synchronize on something? Thanks a lot for your help, Narendra.
 
Sougata Som
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about not replying earlier but indeed, there's nothing wrong with the code above. The problem was elsewhere due to which the "mail.host" property was not getting set in my application code. Thanks for your time, Narendra!
 
zakur khan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Thread hijack removed: please start your own question in your own new topic.]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!