• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about sending mails with servlets..

 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic