Skip to content

Commit d2172b4

Browse files
authored
1697 - email notification improvements (#1698)
1 parent 698e83a commit d2172b4

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.opendatadiscovery.oddplatform.notification.config;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
@ConfigurationProperties("notifications.receivers.email")
7+
@Data
8+
public class EmailSenderProperties {
9+
private String sender;
10+
private String password;
11+
private String host;
12+
private int port;
13+
private String protocol;
14+
private SmtpProperties smtp;
15+
16+
@Data
17+
public static class SmtpProperties {
18+
private Boolean auth;
19+
private Boolean starttls;
20+
}
21+
}

odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/notification/config/NotificationConfiguration.java

+21-17
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
@Configuration
2727
@ConditionalOnNotifications
28-
@EnableConfigurationProperties(NotificationsProperties.class)
28+
@EnableConfigurationProperties({NotificationsProperties.class, EmailSenderProperties.class})
2929
public class NotificationConfiguration {
3030

3131
@Bean
@@ -35,34 +35,38 @@ public HttpClient httpClient() {
3535

3636
@Bean
3737
@ConditionalOnProperty(name = "notifications.receivers.email.sender")
38-
public JavaMailSender mailSender(@Value("${notifications.receivers.email.sender}") final String senderEmail,
39-
@Value("${notifications.receivers.email.password}") final String senderPassword,
40-
@Value("${notifications.receivers.email.smtp}") final String smtpHost,
41-
@Value("${notifications.receivers.email.port}") final int port) {
42-
if (StringUtils.isBlank(senderEmail)) {
38+
public JavaMailSender mailSender(final EmailSenderProperties emailProperties) {
39+
if (StringUtils.isBlank(emailProperties.getSender())) {
4340
throw new IllegalArgumentException("senderEmail is empty");
4441
}
4542

46-
if (StringUtils.isBlank(senderPassword)) {
47-
throw new IllegalArgumentException("senderPassword is empty");
43+
if (StringUtils.isBlank(emailProperties.getHost())) {
44+
throw new IllegalArgumentException("host is empty");
4845
}
4946

50-
if (StringUtils.isBlank(smtpHost)) {
51-
throw new IllegalArgumentException("smtpHost is empty");
47+
if (StringUtils.isBlank(emailProperties.getProtocol())) {
48+
throw new IllegalArgumentException("protocol is empty");
5249
}
5350

5451
final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
5552

56-
mailSender.setHost(smtpHost);
57-
mailSender.setPort(port);
58-
mailSender.setUsername(senderEmail);
59-
mailSender.setPassword(senderPassword);
53+
mailSender.setHost(emailProperties.getHost());
54+
mailSender.setPort(emailProperties.getPort());
55+
mailSender.setUsername(emailProperties.getSender());
56+
57+
if (emailProperties.getPassword() != null) {
58+
mailSender.setPassword(emailProperties.getPassword());
59+
}
6060

6161
final Properties props = mailSender.getJavaMailProperties();
6262

63-
props.put("mail.transport.protocol", "smtp");
64-
props.put("mail.smtp.auth", "true");
65-
props.put("mail.smtp.starttls.enable", "true");
63+
if (emailProperties.getProtocol().equals("smtp")) {
64+
props.put("mail.transport.protocol", "smtp");
65+
props.put("mail.smtp.auth", emailProperties.getSmtp().getAuth());
66+
props.put("mail.smtp.starttls.enable", emailProperties.getSmtp().getStarttls());
67+
} else {
68+
props.put("mail.transport.protocol", emailProperties.getProtocol());
69+
}
6670

6771
return mailSender;
6872
}

odd-platform-api/src/main/resources/application.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,12 @@ notifications:
185185
# email:
186186
# sender:
187187
# password:
188-
# smtp:
188+
# host:
189189
# port:
190+
# protocol:
191+
# smtp:
192+
# auth:
193+
# starttls:
190194
# notification:
191195
192196

0 commit comments

Comments
 (0)