Skip to content

Commit 9d0b9f6

Browse files
committed
Adding timeouts to SMTP transport sockets for issue bbottema#85
1 parent cf98c0b commit 9d0b9f6

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/main/java/org/simplejavamail/mailer/Mailer.java

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.simplejavamail.mailer.config.TransportStrategy;
1111
import org.simplejavamail.mailer.internal.mailsender.MailSender;
1212
import org.simplejavamail.converter.internal.mimemessage.MimeMessageHelper;
13+
import org.simplejavamail.util.ConfigLoader;
14+
import org.simplejavamail.util.ConfigLoader.Property;
1315
import org.slf4j.Logger;
1416
import org.slf4j.LoggerFactory;
1517

@@ -83,6 +85,12 @@ public class Mailer {
8385

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

88+
/**
89+
* the default maximum timeout value for the transport socket is {@value #DEFAULT_SEND_MAIL_SOCKET_TIMEOUT}
90+
* milliseconds. Can be overridden from a config file or through System variable.
91+
*/
92+
private static final String DEFAULT_SEND_MAIL_SOCKET_TIMEOUT = "60000";
93+
8694
private final MailSender mailSender;
8795

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

240+
// socket timeouts handling
241+
String sendMailTimeoutInMillis = ConfigLoader.valueOrProperty(
242+
null, Property.DEFAULT_SEND_MAIL_TIMEOUT_IN_MILLIS, DEFAULT_SEND_MAIL_SOCKET_TIMEOUT
243+
);
244+
props.put("mail.smtp.connectiontimeout", sendMailTimeoutInMillis);
245+
props.put("mail.smtp.timeout", sendMailTimeoutInMillis);
246+
props.put("mail.smtp.writetimeout", sendMailTimeoutInMillis);
247+
232248
if (serverConfig.getUsername() != null) {
233249
props.put(transportStrategy.propertyNameUsername(), serverConfig.getUsername());
234250
}

src/main/java/org/simplejavamail/mailer/internal/mailsender/MailSender.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public void run() {
168168
try {
169169
sendMailClosure(session, email);
170170
} catch (Exception e) {
171-
LOGGER.trace("could not send email '{}' to {}", email.getSubject(), email.getRecipients(), e);
171+
LOGGER.error("could not send email {}", email, e);
172172
}
173173
}
174174
});

src/main/java/org/simplejavamail/util/ConfigLoader.java

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public enum Property {
9494
DEFAULT_BCC_NAME("simplejavamail.defaults.bcc.name"),
9595
DEFAULT_BCC_ADDRESS("simplejavamail.defaults.bcc.address"),
9696
DEFAULT_POOL_SIZE("simplejavamail.defaults.poolsize"),
97+
DEFAULT_SEND_MAIL_TIMEOUT_IN_MILLIS("simplejavamail.defaults.sendmailtimeoutinmillis"),
9798
TRANSPORT_MODE_LOGGING_ONLY("simplejavamail.transport.mode.logging.only");
9899

99100
private final String key;

0 commit comments

Comments
 (0)