Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrying sending an email with messaging exception

 
Abhishek Mhptr
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have written a code to send an email through SMTP server.
i make use of javax.mail APIs to achieve the same.

My code looks like the below example code:
Sending mail through java

Sometimes when the server is busy i get exception like the following:

1. java.net.SocketTimeoutException : 421 Microsoft ESMTP MAIL Service, Version: 6.0.3790.4675 ready at Service not available, closing transmission channel

2. javax.mail.MessagingException : Could not connect to SMTP host: hostName , port: 25, response: 421

Now i need to handle these exception with a retrying option.

Kindly suggest me whats the best option to retry sending the mail.
While retrying too, there might be exception again. Can we handle the same?


Thanks in Advance.
 
Tim Moores
Bartender
Posts: 2739
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't look at the code, but one approach would be to run the mail sending in a loop until there are no exceptions, putting the current thread to sleep each time there is an exception - may be for 30 seconds or so. This assumes that the mail sending is done in a thread that's separate from the main execution thread, but that's good practice anyway for network operations for which you don't need (or won't get) an immediate answer.
 
Abhishek Mhptr
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim for your valuable input.

The problem here is: i can't make the thread to sleep for 30 seconds.
There is an UI attached for sending mails. The user needs to know about the success or failure of Email.

The process has to be fasten a little.

 
Tim Moores
Bartender
Posts: 2739
36
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way you can provide reliable, immediate feedback for sending email. The host might be down, in which case the email sending is delayed by 4 hours or more. That's how SMTP works, no way around that.
 
Abhishek Mhptr
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah. True Tim.. But the idea is to retry sending mail for few times (Lets say 5-6 times) when an exception received in the catch block.

If the exception occurs all the time, i would stop retrying and show a failure message.
 
Tim Moores
Bartender
Posts: 2739
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then use a proper mail server for sending. That way you'll get notifications emails if the mail is delayed, and bounces if it doesn't go through at all. Retrying to send email in a short time frame is unlikely to yield better results than letting SMTP and the mail server handle all that for you.
 
Abhishek Mhptr
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah.. I don't get this exception in all servers.(They must be properly handling the requests.)
In a particular server, i get this exception while doing continuous testing. (421 response comes when the server is busy)

My concern is only for this particular server. I just need to retry few times.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic