It's not a secret anymore!*
The moose likes Servlets and the fly likes A question about sending mails with servlets.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "A question about sending mails with servlets.." Watch "A question about sending mails with servlets.." New topic
Author

A question about sending mails with servlets..

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi fellows. I have class with static methods that I'm using to send emails using servlets. So, whenever I need to send a mail, I do.
MyMailer.send(email,subject,etc,etc)
and that's it, it is working fine. I was wondering what happens if the email recipient is not available at the moment, or if the email address is not valid, or any other error that occurs while sending email. The servlet might have executed that line and these sort of errors might occurr later.
So, is this the way to do it? I thought of probably having a separate thread that does the emailing stuff while my servlet is executed normally. but I'm not sure about this..
What happens for example if you're implementing a notification service and have to send hundreds of emails to different recipients, how is this done?
hope you guys understand my question...
thanks


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
By all means, you should hand off mail sending to a separate Thread that can handle error reporting etc. without interfering with the servlet Thread. Just make MyMailer.send add the data to a queue and have a low priority Thread work from that Queue. Ideal for your hypothetical notification system, you could hand MyMailer a list of addresses.
As you say, there are many things that can go wrong with email - you do NOT want to hang up a servlet Thread waiting for a mail processor.
Bill
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
guys.. it's not working, now it is not even sending the email . Am I doing it right?

and in my servlet I only do:
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by William Brogden:
By all means, you should hand off mail sending to a separate Thread that can handle error reporting etc. without interfering with the servlet Thread. Just make MyMailer.send add the data to a queue and have a low priority Thread work from that Queue. Ideal for your hypothetical notification system, you could hand MyMailer a list of addresses.
As you say, there are many things that can go wrong with email - you do NOT want to hang up a servlet Thread waiting for a mail processor.
Bill

However, writing these "worker thread" implementations is somewhat risky (and frowned upon, as you shouldn't really try to manage threads inside an application server). I've done it myself in the past and it has worked fine. But, what happens if the server goes down while the queue is not empty? You can potentially lose one of your "tasks". Granted, that's somewhat unlikely, but for an application that requires that all tasks placed on the queue must be completed, it may be a risk they don't want to take. A better solution, if the messages HAVE to be delivered is to use JMS (if you have it available on your server). Just my $0.02.


James Carman, President<br />Carman Consulting, Inc.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
Tch tch! You will never find out whats going wrong with this code:

because you are throwing away any exception that might tell you what is going wrong.
[ September 06, 2002: Message edited by: William Brogden ]
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Thank you guys.. one last question.. is my thread finishing when my for loop is done? in other words, how do I stop the thread? is it automatically finished when it sends all the emails..?
thanks again
 
jQuery in Action, 2nd edition
 
subject: A question about sending mails with servlets..