How to test that the mail server is alive with Jav

2020-07-06 04:39发布

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.

标签: java javamail
2条回答
混吃等死
2楼-- · 2020-07-06 04:46

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.

查看更多
乱世女痞
3楼-- · 2020-07-06 04:51

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));
查看更多
登录 后发表回答