How to Check E-Mail Server Connection

These days I used the following snippet to get in touch with a new E-Mail provider.

try {
  Properties props = new Properties();
  props.put("mail.smtp.starttls.enable", "true");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.ssl.trust", mailConfig.getHost());

  Session session = Session.getInstance(props, null);
  Transport transport = session.getTransport("smtp");
  transport.connect(mailConfig.getHost(), mailConfig.getPort(), mailConfig.getAuthenticationUser(),
                 mailConfig.getAuthenticationPassword());
  transport.close();
  result = true;
} catch (AuthenticationFailedException e) {
  LOG.error("Failed to test E-Mail server configuration.", e);
} catch (MessagingException e) {
  LOG.error("Failed to test E-Mail server configuration.", e);
}

After some minutes of guessing I had a deja-vue and lucky me remembered from a former code review that there is a possibility to enable debugging on a SMTP session. It was coupled to the DEBUG setting of the standard logging:

if (LOG.isDebugEnabled()) {
    session.setDebug(true);
    session.setDebugOut(new PrintStream(System.out) {
        @Override
        public void print(String s) {
            LOG.debug(s);
        }
    });
}

With the additional log information we gained new insights and were able to overcome the initial problems easily.