• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Retrying sending an email with messaging exception

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 7645
178
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 7645
178
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After searching through internet never found a proper solution, just doing for loop, in Exception block catching exception stack, if no exception null
if not null, again next iteration in for loop
each iteration if not null, add it to List<String> for audit
and make this whole thing async if possible as one round may take around 7/8 seconds
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic