File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes javamail: method blocks instead of throwing exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "javamail: method blocks instead of throwing exception" Watch "javamail: method blocks instead of throwing exception" New topic
Author

javamail: method blocks instead of throwing exception

Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Hello,

I have the following code in a Swing/Spring application:


When the email address I try and send the message to is erroneous i.e. julien@domain or julien@domainDoesNotExist.com the above method blocks indefinitely without throwing any exception...

Note that it runs within a SwingWorker.

Can anyone please help?

Thanks in advance,

Julien.
Ramon Anger
Ranch Hand

Joined: Apr 19, 2011
Posts: 56

Hi Julien,

why not throw your own TimeoutException after x seconds?

Cheers,
Ramon


Blackbelt on BlackBeltFactory.com.
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Hello Ramon,
I altered my app as follows:


Now I do rethrow a RuntimeException. However it still blocks and no rollback is performed...
Any other idea?
J.
Ramon Anger
Ranch Hand

Joined: Apr 19, 2011
Posts: 56

Hi Julien,

I'm afraid this is not exactly what I meant. Please try the following:

1. Sending your mails has to happen in its own Thread.
2. In case your mail sending thread has successfully send the mail, you set a variable back in your main thread.
3. In your main thread you set a timer or something that realizes how many time has been spent.
4. After your time limit has been reached, you could check whether the mail sending thread set that variable yet (see 2.). If not, stop that thread and you could go on in your main thread.

BTW: Are you sure, that the mail connection you use to send that email, has no timeout parameter that can be set?

Cheers,
Ramon
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Good point Ramon.
First: I dropped the thread implementation: it now execute sequentially.
Second: upon your advice, I added this to my javamailsender bean:

I have the same behavior as before.
Julien.
Ramon Anger
Ranch Hand

Joined: Apr 19, 2011
Posts: 56

Hi Julien,

a value of 1000 would mean timeout after 1 second.
Can you ensure that the properties you've set are known to your mail session at runtime?
There should be a Session.getProperties() method and/or Session.getProperty("mail.smtp.connectiontimeout") method.

If not set, mail.smtp.connectiontimeout and mail.smtp.timeout are both infinite and that's the behaviour your code shows ...

Cheers,
Ramon
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
I checked that my JavaMailSenderImpl does have a getJavaMailProperties method and also that the values are in the mailSender's session.

and

However, it does seem those properties are ignored...
Regards,
Julien.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

If you're trying to debug JavaMail, one of the recommended things to try is turning on debug mode, so you can see the conversation between your code and the server.

However you seem to be trying to debug JavaMail and Spring at the same time. I recommend putting Spring aside and debugging JavaMail by itself to start with, otherwise you don't know whether you are debugging JavaMail or Spring.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javamail: method blocks instead of throwing exception