aspose file tools*
The moose likes Tomcat and the fly likes JavaMail w/ Tomat problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "JavaMail w/ Tomat problem" Watch "JavaMail w/ Tomat problem" New topic
Author

JavaMail w/ Tomat problem

Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Hi All,
I have successfully used the JavaMail program from a straight java program, but am having a hard time using it from my tomcat servlet.


I have the mail-1.4.1 jar and activation.jar in my tomcat\common\lib folder.

I have read the javamail faq's where they suggest trying to telnet in to the host, when I do, I am able to connect successfully to the host.

When I turn session.debug(true), I see the following error:
DEBUG: setDebug: JavaMail version 1.4.1ea-SNAPSHOT
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
DEBUG SMTP: exception reading response: java.net.SocketException: Connection reset


What I don't understand about this message is why it says "trying to connect to host "localhost" when I give the mail host name in my context.xml as :


Shouldn't the debug message say that it is trying to connect to "mycompanyName" on port 25? It looks like maybe tomcat is not seeing my configuration in context.xml and using some default values.

Here is my java code:


Any help very appreciated.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
I've never configured JavaMail as a web app resource, but this:

looks suspect (note how it says "useAuth false" in the debug output). Maybe that causes it to disregard the other settings as well. Something like the following might work better:

[ March 17, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Thanks for your quick reply.

Yes, I see that Tomcat did not seem to "like" my context.xml file, so I changed it to be this:


This allowed me to get past that first error I was having, but now I find I am hitting the following authentication error:

DEBUG: setDebug: JavaMail version 1.4.1ea-SNAPSHOT
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:319)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)


So, getting closer! Can anyone see a problem with how I am defining my username and password for authentication? I use mail.stmp.user="name.alerts@mycompany.com" , maybe I should just say user="name.alerts@mycompany.com" or mail.stmp.user="name.alerts" ? I am sure it is some simple syntax mistake like this.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
maybe I should just say user="name.alerts@mycompany.com" or mail.stmp.user="name.alerts"?[/QB]


Or use mail.smtp.user and mail.smtp.password instead
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Doh!
Thanks so much! Very helpful to have a second set of eye's to see these stupid typo's. Once I changed my stmp's to smtp's, I was able to pass my authentication problem. However, now I am having troupble with JavaMail not likeing the address I am sending mail TO.

It will send mail fine TO any address on the same host "myCompany.com" that I am sending mail FROM, but if I try to send email to a differant host "name@anotherDomain.com", I get an invalid destination address exception.



So, once again, getting further, but still hitting problems. Thanks for sticking with me on this problem.

Here is my current config :

Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Sorry,
My mistake on last post, I just realized that I had set mail.smtp.auth="false" as a test, once I set it back to "true", now I am getting the AuthenticationFailedException again.
Argh.
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
OK, I was able to get this to work by brute force.

I changed the way I used the Transport object. Everything in the documentation said to use the static send method of the Transport object, however, this would not work so I created an instance of it, gave it my host, user, and password values, and was able to send the emails.

Here is the code in case anyone else hit's this problem :

 
Consider Paul's rocket mass heater.
 
subject: JavaMail w/ Tomat problem