Add mail server connection check
If a `JavaMailSenderImpl` is available, check that the underlying mail server is available on startup. Add a `spring.mail.test-connection` property to control this behaviour. Closes gh-3408
This commit is contained in:
parent
0eada5df86
commit
6f31080f78
|
|
@ -67,6 +67,11 @@ public class MailProperties {
|
||||||
*/
|
*/
|
||||||
private String jndiName;
|
private String jndiName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ping to mail server while initializing.
|
||||||
|
*/
|
||||||
|
private boolean testConnection;
|
||||||
|
|
||||||
public String getHost() {
|
public String getHost() {
|
||||||
return this.host;
|
return this.host;
|
||||||
}
|
}
|
||||||
|
|
@ -119,4 +124,11 @@ public class MailProperties {
|
||||||
return this.jndiName;
|
return this.jndiName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isTestConnection() {
|
||||||
|
return this.testConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTestConnection(boolean testConnection) {
|
||||||
|
this.testConnection = testConnection;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.activation.MimeType;
|
import javax.activation.MimeType;
|
||||||
|
import javax.mail.MessagingException;
|
||||||
import javax.mail.Session;
|
import javax.mail.Session;
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
|
|
||||||
|
|
@ -69,9 +70,21 @@ public class MailSenderAutoConfiguration {
|
||||||
else {
|
else {
|
||||||
applyProperties(sender);
|
applyProperties(sender);
|
||||||
}
|
}
|
||||||
|
validateConnection(sender);
|
||||||
return sender;
|
return sender;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validateConnection(JavaMailSenderImpl sender) {
|
||||||
|
if (this.properties.isTestConnection()) {
|
||||||
|
try {
|
||||||
|
sender.testConnection();
|
||||||
|
} catch (MessagingException ex) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
String.format("Unable to ping to %s", this.properties.getHost()), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void applyProperties(JavaMailSenderImpl sender) {
|
private void applyProperties(JavaMailSenderImpl sender) {
|
||||||
sender.setHost(this.properties.getHost());
|
sender.setHost(this.properties.getHost());
|
||||||
if (this.properties.getPort() != null) {
|
if (this.properties.getPort() != null) {
|
||||||
|
|
|
||||||
|
|
@ -519,6 +519,7 @@ content into your application; rather pick only the properties that you need.
|
||||||
spring.mail.default-encoding=UTF-8 # encoding to use for MimeMessages
|
spring.mail.default-encoding=UTF-8 # encoding to use for MimeMessages
|
||||||
spring.mail.properties.*= # properties to set on the JavaMail session
|
spring.mail.properties.*= # properties to set on the JavaMail session
|
||||||
spring.mail.jndi-name= # JNDI location of a Mail Session
|
spring.mail.jndi-name= # JNDI location of a Mail Session
|
||||||
|
spring.mail.test-connection= # Enable test connection. Default: false
|
||||||
|
|
||||||
# SPRING BATCH ({sc-spring-boot-autoconfigure}/batch/BatchProperties.{sc-ext}[BatchProperties])
|
# SPRING BATCH ({sc-spring-boot-autoconfigure}/batch/BatchProperties.{sc-ext}[BatchProperties])
|
||||||
spring.batch.job.names=job1,job2
|
spring.batch.job.names=job1,job2
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue