这是我创建的类,用于检查SMTP服务是否在主机的特定端口上运行。我在尝试连接时收到了IOException(我已在评论中提到过)。我是否正确地做到了这一点。是否有其他更好的方法来确定服务是否在特定端口的主机中完美运行。我正在使用apache commons库。
import org.apache.commons.net.smtp.AuthenticatingSMTPClient;
import java.io.IOException;
import java.net.SocketException;
import java.security.NoSuchAlgorithmException;
public class SmtpServiceTest {
String hostaddress;
Integer port;
public SmtpServiceTest(String hostaddress, Integer port) {
this.hostaddress = hostaddress;
this.port = port;
}
public Boolean execute() {
AuthenticatingSMTPClient client = null;
try {
client = new AuthenticatingSMTPClient();
client.setDefaultTimeout(10 * 1000);
// I get IOException on this line while trying to connect
client.connect(hostaddress, port);
// I am not sure about this too
if (client.execTLS()) {
return true;
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(client != null && client.isConnected()) {
try {
client.logout();
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
}