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

前端 未结 3 1966
长发绾君心
长发绾君心 2021-02-20 12:01

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.

3条回答
  •  粉色の甜心
    2021-02-20 12:35

    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;
    }
    

提交回复
热议问题