Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test that the mail server is alive with Java?

Is there a way with JavaMail API to check that the mail server used is alive ? If not, how do to it with Java code ?

Thanks by advance for your help.

like image 361
Denis R. Avatar asked Feb 15 '12 14:02

Denis R.


People also ask

How do you test if SMTP is working on a server?

Test the SMTP Service Telnet at a command prompt, and then press ENTER. At the telnet prompt, type set LocalEcho, press ENTER, and then type open <machinename> 25, and then press ENTER.

How does JavaMail API work?

The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with the Java SE platform and is also included in the Java EE platform.


2 Answers

From this Link; you can use the following logic:

public boolean isAlive() throws MessagingException {
  session.setDebug(true);
  Transport transport = session.getTransport("smtp");
  transport.connect();
  if (transport.isConnected()) {
    transport.close();
    return true;
  }
  return false;
}
like image 192
YourAboutMeIsBlank Avatar answered Sep 21 '22 15:09

YourAboutMeIsBlank


If you've got a reference to a Session instance, you could do the following:

Session s = //a JavaMail session I got from somewhere
boolean isConnected = s.getTransport("smtp").isConnected();

If the mail client is connected to the appropriate SMTP server, it usually means it's alive.

like image 27
Sean Reilly Avatar answered Sep 19 '22 15:09

Sean Reilly