Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

Mail API & Retrying Host

I have a working app that send mail successfully when the mail server is ok (obvious so far!).

What I want to be able to do is detect that the host is not available and retry with a second host.

I'd like to be able to detect this before sending a message, such as in the code:

Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);

Where props contains the host name.

I know that the host could fail at any point after this, and during the sending of a particular message.

It would be good to be able to catch an exception (MessagingException?) at the point of making the host connection, and if it fails, try another host. It would be even better to be able to detect the failed host connection during sending each message and retry with another host, but I think that is more difficult, given the connection has already been made.

Any ideas?
Thanks
0
harrywillis
Asked:
harrywillis
1 Solution
 
Venci75Commented:
you can try something like this:

try {
  Session session = Session.getInstance(props, null);
  session.getStore("imap").connect(host,username,password);
} catch (MessagingException e) {
  // you will be here if 'connect' has failed
}
0
 
harrywillisAuthor Commented:
Thanks. I want something more like:

try {
  Session session = Session.getInstance(props, null);
  session.getStore("imap").connect(host,username,password);
} catch (MessagingException e) {
  // try connection to 2nd host
}

Or even better, when sending a message and the host cannot be contacted, to retry with another host.
0
 
rahul_ashishCommented:
well if Session.getInstance() fails then session will be null. u can check for the null condition and use a While loop to connect to other host. this ways u need only one try and catch and u will be saved form nested try and catch. when connection is sucessful u can break out of while loop.
0
 
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this
question is:

- points to Venci75

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now