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

微笑、不失礼 提交于 2020-01-02 02:42:09

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/9294968/how-to-test-that-the-mail-server-is-alive-with-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!