This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Retrying sending an email with messaging exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Retrying sending an email with messaging exception " Watch "Retrying sending an email with messaging exception " New topic
Author

Retrying sending an email with messaging exception

Abhishek Mhptr
Ranch Hand

Joined: Aug 09, 2009
Posts: 33
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Aug 09, 2009
Posts: 33
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Aug 09, 2009
Posts: 33
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Aug 09, 2009
Posts: 33
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrying sending an email with messaging exception
 
Similar Threads
Java Mail
how to know the domain names of the smtp servers
java mail error
Simple Java Mail : Relaying Denied
Javamail problem