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

前端 未结 3 1994
长发绾君心
长发绾君心 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

    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.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-02-20 12:48

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

    From a connectivity standpoint, you could just ping it:

      InetAddress host = InetAddress.getByName("mailserver");
      System.out.println("host.isReachable(1000) = " + host.isReachable(1000));
    
    0 讨论(0)
提交回复
热议问题