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.
Test the SMTP Service Telnet at a command prompt, and then press ENTER. At the telnet prompt, type set LocalEcho, press ENTER, and then type open <machinename> 25, and then press ENTER.
The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with the Java SE platform and is also included in the Java EE platform.
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With