1
properties.put("mail.imap.host", host);
properties.put("mail.imap.port", "993");
Session emailSession = Session.getDefaultInstance(properties);
Store store = emailSession.getStore("imaps");
store.connect(host, username, password);

This code is working well, but if the mail server is down, I got the expection:

javax.mail.MessagingException: * BYE JavaMail Exception: java.io.IOException: Connection dropped by server?;
nested exception is:
com.sun.mail.iap.ConnectionException: * BYE JavaMail Exception: java.io.IOException: Connection dropped by server?
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:731)
at javax.mail.Service.connect(Service.java:366)
at javax.mail.Service.connect(Service.java:246)
at eugene.neocortex.fm.mail.check.CheckingMails.fetch(CheckingMails.java:100)

So I need to check if server is available? And I read the documentation of javax.mail and cant find method, how do I can check if server is alive? Method isConnected() checks if currently connected, but I need to check connection to my mail server before this code or I got exception again on this code string:

Store store = emailSession.getStore("imaps");
Eugene Shamkin
  • 163
  • 1
  • 3
  • 13
  • Refer this link - https://stackoverflow.com/questions/9294968/how-to-test-that-the-mail-server-is-alive-with-java. Hopefully this code helps - `InetAddress host = InetAddress.getByName("mailserver"); System.out.println("host.isReachable(1000) = " + host.isReachable(1000)); ` – Ramesh Subramanian Nov 20 '19 at 05:19
  • @RameshSubramanian That doesn't test the mail connection, only the network path to the host. – user207421 Nov 20 '19 at 08:02
  • You don't. You try to use the connection and recover when you find it fails. There is no magic trick that will let you see whether a connection, or indeed any other resource, is *about* to work. – user207421 Nov 20 '19 at 08:03

0 Answers0