wood burning stoves 2.0*
The moose likes Other JSE/JEE APIs and the fly likes Java Mail API Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java Mail API Help" Watch "Java Mail API Help" New topic
Author

Java Mail API Help

sam wootton
Ranch Hand

Joined: Apr 16, 2011
Posts: 68
    
    1
Hi,

As always, many thanks for anyones help or advice.

I am trying to send email from my Servlet.



I have:



In catalina.policy i have:



No exception or error is being thrown, but i do not get an email sent to my address.

Im not sure why this isn't working.

I am using java 1.6, so i shouldn't need anything mnore than mail.jar.

Regards, Sam
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Call session.setDebug(true) and check your output / log files. You'll be able to see exactly what JavaMail is doing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18709
    
    8

I would suggest that you test sending of e-mail in a plain old Java application first, like a small command-line program which does just that and nothing else. Tomcat and Servlets aren't really easy to use for basic testing.

And make sure that your setup agrees with the assumptions that I see from your post:

1. You are running an SMTP server on the same computer where the code is running.

2. It is listening at port 25 (which is the default port for SMTP).

3. It doesn't require authentication.

4. It is configured so that it can relay e-mails which claim to come from a different domain (gmail.com in your case).

You should also become conversant with the administrative functions of that SMTP server, so that you can tell whether it has delivered an e-mail successfully. Or if it isn't your server, you should enlist the help of its administrator.
sam wootton
Ranch Hand

Joined: Apr 16, 2011
Posts: 68
    
    1
[Many thanks for your help.

I think the problem could be my local host sending. I thought i could just use googles smtp server. I did used to run postfix... but it was such a pain to administer, i ditched it.



So you cant send email from java, without the ocal machine running an SMTP server?


Regards, Sam
sam wootton
Ranch Hand

Joined: Apr 16, 2011
Posts: 68
    
    1
I guess if i had host as "smtp.gmail.com"



Regards, Sam
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18709
    
    8

Sure you can use the Gmail server. Have a look at the JavaMail FAQ for the issues involved with doing that, including how to do the authentication.
sam wootton
Ranch Hand

Joined: Apr 16, 2011
Posts: 68
    
    1
Hi,

Massive thanks!!! Got it all working great.



Many thanks for everyones help.

Regards, Sam

(Edit: Hide password -- Paul C)
(Edit: and email address -- Rob)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18709
    
    8

Working now? Great!

However...



You didn't get this from the JavaMail FAQ, did you? Because there's no such property in JavaMail.

There's nothing wrong with using non-existent properties, in the sense that they don't cause the program to break. But it's unhelpful to future maintainers of the code because it gives them extra work to find out that they do nothing. Likewise it's unhelpful to post such code on the web because then unwary students will copy the code elsewhere, compounding the problem.
sam wootton
Ranch Hand

Joined: Apr 16, 2011
Posts: 68
    
    1
Ah thanks Paul Clapham, no i didnt. tbh, it was a mix of googling / forums / mail API reading. I started looking prior to reading your previous response.

Many thanks for your help, much appreciated!

Out of interest - how safe is storing my email pwd as compiled code, on tomcat?

I'm not that happy with it, or am i just paranoid?

Regards, Sam
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Depends - do you trust your sys admins? If not, I suggest to get hosting elsewhere.
sam wootton
Ranch Hand

Joined: Apr 16, 2011
Posts: 68
    
    1
Tim Moores

well, currently its just running locally (on Opensuse) - so i trust me.

the client-side is actually a swing desktop application, and i guess in the future it would have to use either the chosen hosts mx servers, or my own running next to tomcat. But thats a long way off (and this is only a peronsal project, so whether i ever get to that point is in question).

thanks again for your help.

I more wondered if it was possibleto hack a servlet to get info like that.

Regards, Sam
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
If you somehow obtain physical access to the servlet's class/jar file then it's easy to decompile it and retrieve such information. But I'd argue that once someone has access to those files, and is bent on causing trouble, you have much bigger things to worry about.

In general, you should keep the connection details out of the Java code, maybe in a properties file, just like you'd do for DB and logging properties.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Mail API Help