[ACCEPTED]-How to test that the mail server is alive with Java?-jakarta-mail

Accepted answer
Score: 11

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

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

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

Score: 5

From the JavaMail API, you could try sending 3 an email and seeing if it was sent successfully.

From 2 a connectivity standpoint, you could just 1 ping it:

  InetAddress host = InetAddress.getByName("mailserver");
  System.out.println("host.isReachable(1000) = " + host.isReachable(1000));
Score: 1

From this Link; you can use the following logic:

public boolean isAlive() throws MessagingException {
  Transport transport = session.getTransport("smtp");
  if (transport.isConnected()) {
    return true;
  return false;


More Related questions