Skip to content

Commit

Permalink
Add support for SSL and debug in email error handler (#1507)
Browse files Browse the repository at this point in the history
  • Loading branch information
TeslaCN committed Sep 28, 2020
1 parent 25661f2 commit fe0617d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
Expand Up @@ -62,17 +62,25 @@ public static EmailConfiguration buildConfigBySystemProperties() {
emailConfiguration.setCc(System.getProperty("error-handler-email.cc"));
emailConfiguration.setBcc(System.getProperty("error-handler-email.bcc"));
String protocol = System.getProperty("error-handler-email.protocol");
String subject = System.getProperty("error-handler-email.subject");
String port = System.getProperty("error-handler-email.port");
if (StringUtils.isNotBlank(protocol)) {
emailConfiguration.setProtocol(System.getProperty("error-handler-email.protocol"));
}
String useSsl = System.getProperty("error-handler-email.use-ssl");
if (StringUtils.isNotBlank(useSsl)) {
emailConfiguration.setUseSsl(Boolean.parseBoolean(useSsl));
}
String subject = System.getProperty("error-handler-email.subject");
if (StringUtils.isNotBlank(subject)) {
emailConfiguration.setSubject(subject);
}
String port = System.getProperty("error-handler-email.port");
if (StringUtils.isNotBlank(port)) {
emailConfiguration.setPort(Integer.valueOf(port));
}
String debug = System.getProperty("error-handler-email.debug");
if (StringUtils.isNotBlank(debug)) {
emailConfiguration.setDebug(Boolean.parseBoolean(debug));
}
return emailConfiguration;
}
}
Expand Up @@ -37,6 +37,8 @@ public final class EmailConfiguration {

private String protocol = "smtp";

private boolean useSsl;

private String subject = "ElasticJob error message";

private String from;
Expand All @@ -46,4 +48,6 @@ public final class EmailConfiguration {
private String cc;

private String bcc;

private boolean debug;
}
Expand Up @@ -37,6 +37,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.Optional;
import java.util.Properties;

/**
Expand Down Expand Up @@ -79,13 +80,18 @@ public String getType() {
return "EMAIL";
}

private Session buildSession() {
private synchronized Session buildSession() {
if (null == session) {
Properties props = new Properties();
props.put("mail.smtp.host", emailConfiguration.getHost());
props.put("mail.smtp.port", emailConfiguration.getPort());
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", emailConfiguration.getProtocol());
props.setProperty("mail.debug", Boolean.toString(emailConfiguration.isDebug()));
if (emailConfiguration.isUseSsl()) {
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
}
session = Session.getDefaultInstance(props, new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
Expand All @@ -97,7 +103,7 @@ public PasswordAuthentication getPasswordAuthentication() {
}

private Message buildMessage(final String content) throws MessagingException {
MimeMessage message = new MimeMessage(buildSession());
MimeMessage message = new MimeMessage(Optional.ofNullable(session).orElseGet(this::buildSession));
message.setFrom(new InternetAddress(emailConfiguration.getFrom()));
message.setSubject(emailConfiguration.getSubject());
message.setSentDate(new Date());
Expand Down
Expand Up @@ -21,7 +21,9 @@ email:
username: username
password: password
protocol: smtp
useSsl: true
from: xxx
to: xxx
cc: xxx
bcc: xxx
debug: true
Expand Up @@ -29,6 +29,7 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -55,6 +56,8 @@ public void assertHandleExceptionWithYAMLConfiguration() {
assertThat(emailConfiguration.getBcc(), equalTo("yaml.bcc@ejob.com"));
assertThat(emailConfiguration.getProtocol(), equalTo("yaml.smtp"));
assertThat(emailConfiguration.getSubject(), equalTo("yaml.subject"));
assertTrue(emailConfiguration.isUseSsl());
assertTrue(emailConfiguration.isDebug());
}

@Test
Expand All @@ -74,6 +77,8 @@ public void assertHandleExceptionWithSystemPropertiesConfiguration() {
assertThat(emailConfiguration.getTo(), equalTo("system.to@ejob.com"));
assertThat(emailConfiguration.getCc(), equalTo("system.cc@ejob.com"));
assertThat(emailConfiguration.getProtocol(), equalTo("smtp"));
assertTrue(emailConfiguration.isUseSsl());
assertTrue(emailConfiguration.isDebug());
}

@Test
Expand Down Expand Up @@ -109,13 +114,16 @@ private void initSystemProperties() {
System.setProperty("error-handler-email.use-system-properties", "true");
System.setProperty("error-handler-email.host", "system.email.com");
System.setProperty("error-handler-email.port", "345");
System.setProperty("error-handler-email.protocol", "smtp");
System.setProperty("error-handler-email.use-ssl", "true");
System.setProperty("error-handler-email.username", "system.username");
System.setProperty("error-handler-email.password", "system.password");
System.setProperty("error-handler-email.subject", "system.subject");
System.setProperty("error-handler-email.from", "system.from@ejob.com");
System.setProperty("error-handler-email.to", "system.to@ejob.com");
System.setProperty("error-handler-email.cc", "system.cc@ejob.com");
System.setProperty("error-handler-email.bcc", "system.bcc@ejob.com");
System.setProperty("error-handler-email.debug", "true");
}

private void resetSystemProperties() {
Expand Down
Expand Up @@ -26,3 +26,5 @@ email:
to: yaml.to@ejob.com
cc: yaml.cc@ejob.com
bcc: yaml.bcc@ejob.com
useSsl: true
debug: true

0 comments on commit fe0617d

Please sign in to comment.