Skip to content

Add timeouts to SMTP transport sockets #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/org/simplejavamail/mailer/Mailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.simplejavamail.mailer.config.TransportStrategy;
import org.simplejavamail.mailer.internal.mailsender.MailSender;
import org.simplejavamail.converter.internal.mimemessage.MimeMessageHelper;
import org.simplejavamail.util.ConfigLoader;
import org.simplejavamail.util.ConfigLoader.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -83,6 +85,12 @@ public class Mailer {

private static final Logger LOGGER = LoggerFactory.getLogger(Mailer.class);

/**
* The default maximum timeout value for the transport socket is {@value #DEFAULT_MAIL_SOCKET_TIMEOUT}
* milliseconds. Can be overridden from a config file or through System variable.
*/
private static final String DEFAULT_MAIL_SOCKET_TIMEOUT = "60000";

private final MailSender mailSender;

/**
Expand Down Expand Up @@ -229,6 +237,14 @@ public static Session createMailSession(final ServerConfig serverConfig, final T
props.put(transportStrategy.propertyNameHost(), serverConfig.getHost());
props.put(transportStrategy.propertyNamePort(), String.valueOf(serverConfig.getPort()));

// socket timeouts handling
String sendMailTimeoutInMillis = ConfigLoader.valueOrProperty(
null, Property.DEFAULT_MAIL_SOCKET_TIMEOUT_IN_MILLIS, DEFAULT_MAIL_SOCKET_TIMEOUT
);
props.put("mail.smtp.connectiontimeout", sendMailTimeoutInMillis);
props.put("mail.smtp.timeout", sendMailTimeoutInMillis);
props.put("mail.smtp.writetimeout", sendMailTimeoutInMillis);

if (serverConfig.getUsername() != null) {
props.put(transportStrategy.propertyNameUsername(), serverConfig.getUsername());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,21 @@ the proxy bridge server (or connection pool in async mode) while a non-async ema
if (executor == null || executor.isTerminated()) {
executor = Executors.newFixedThreadPool(threadPoolSize);
}
executor.execute(new Thread("sendMail process") {
executor.execute(new Runnable() {
private static final String NAME = "sendMail process";

@Override
public void run() {
sendMailClosure(session, email);
try {
sendMailClosure(session, email);
} catch (Exception e) {
LOGGER.error("could not send email {}", email, e);
}
}

@Override
public String toString() {
return NAME;
}
});
} else {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/simplejavamail/util/ConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public enum Property {
DEFAULT_BCC_NAME("simplejavamail.defaults.bcc.name"),
DEFAULT_BCC_ADDRESS("simplejavamail.defaults.bcc.address"),
DEFAULT_POOL_SIZE("simplejavamail.defaults.poolsize"),
DEFAULT_MAIL_SOCKET_TIMEOUT_IN_MILLIS("simplejavamail.defaults.mailsockettimeoutinmillis"),
TRANSPORT_MODE_LOGGING_ONLY("simplejavamail.transport.mode.logging.only");

private final String key;
Expand Down