Search code examples
javaplayframeworkapache-commons

Apache Commons Email v1.1 change timeout time


I am creating an application with play framework in java. I have the following code to send an email when you register:

SimpleEmail mail = new SimpleEmail();
mail.setHostName(ConfigFactory.load().getString("mail.hostname"));
mail.setSmtpPort(ConfigFactory.load().getInt("mail.port"));
mail.setAuthenticator(new DefaultAuthenticator(ConfigFactory.load().getString("mail.username"), ConfigFactory.load().getString("mail.password")));
mail.setDebug(true);
mail.setMsg("Test");
mail.setTLS(true);
mail.setFrom("emailhere");
mail.addTo(email);
mail.setSubject("Activate your email");
mail.setMsg("message");
mail.send();

Now when the mail server can't be reached it takes quite some times before it gives an error. I want to shorten this time so the user doesn't have to wait as long. How can I do this?

Thanks in advance!


Solution

  • Use mail.setSocketConnectionTimeout (connenction timeout) and mail.setSocketTimeout (read timeout). Example for setting both to 10 seconds:

    mail.setSocketConnectionTimeout(10000);
    mail.setSocketTimeout(10000);