How can I detect when my SMTP server is down? Can it be done by setting a timeout on the Transport.send() call?

Andrea Pompili

You can do it in three ways:

  1. Setting the Timeout in the property file you use to initialize the session.

    mail.smtp.timeout = <timeout>

    This works only for the SUN provider, if you use something else you have to see the documentation of that thirdy-part provider.
  2. Using the method Transport.connect()
    In this case, if the server is down you obtain a MessagingException explaining that. Look also at the Transport.protocolConnect(...) method that does the same thing.
    All the methods are inherited by Service class.
  3. Trying to enstablish a connection to the address/port of your SMTP Server and waiting for response. No answer means it's down...