Skip to content

Commit

Permalink
1697 - email notification improvements (#1698)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladysl authored Aug 28, 2024
1 parent 698e83a commit d2172b4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.opendatadiscovery.oddplatform.notification.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("notifications.receivers.email")
@Data
public class EmailSenderProperties {
private String sender;
private String password;
private String host;
private int port;
private String protocol;
private SmtpProperties smtp;

@Data
public static class SmtpProperties {
private Boolean auth;
private Boolean starttls;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

@Configuration
@ConditionalOnNotifications
@EnableConfigurationProperties(NotificationsProperties.class)
@EnableConfigurationProperties({NotificationsProperties.class, EmailSenderProperties.class})
public class NotificationConfiguration {

@Bean
Expand All @@ -35,34 +35,38 @@ public HttpClient httpClient() {

@Bean
@ConditionalOnProperty(name = "notifications.receivers.email.sender")
public JavaMailSender mailSender(@Value("${notifications.receivers.email.sender}") final String senderEmail,
@Value("${notifications.receivers.email.password}") final String senderPassword,
@Value("${notifications.receivers.email.smtp}") final String smtpHost,
@Value("${notifications.receivers.email.port}") final int port) {
if (StringUtils.isBlank(senderEmail)) {
public JavaMailSender mailSender(final EmailSenderProperties emailProperties) {
if (StringUtils.isBlank(emailProperties.getSender())) {
throw new IllegalArgumentException("senderEmail is empty");
}

if (StringUtils.isBlank(senderPassword)) {
throw new IllegalArgumentException("senderPassword is empty");
if (StringUtils.isBlank(emailProperties.getHost())) {
throw new IllegalArgumentException("host is empty");
}

if (StringUtils.isBlank(smtpHost)) {
throw new IllegalArgumentException("smtpHost is empty");
if (StringUtils.isBlank(emailProperties.getProtocol())) {
throw new IllegalArgumentException("protocol is empty");
}

final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

mailSender.setHost(smtpHost);
mailSender.setPort(port);
mailSender.setUsername(senderEmail);
mailSender.setPassword(senderPassword);
mailSender.setHost(emailProperties.getHost());
mailSender.setPort(emailProperties.getPort());
mailSender.setUsername(emailProperties.getSender());

if (emailProperties.getPassword() != null) {
mailSender.setPassword(emailProperties.getPassword());
}

final Properties props = mailSender.getJavaMailProperties();

props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
if (emailProperties.getProtocol().equals("smtp")) {
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", emailProperties.getSmtp().getAuth());
props.put("mail.smtp.starttls.enable", emailProperties.getSmtp().getStarttls());
} else {
props.put("mail.transport.protocol", emailProperties.getProtocol());
}

return mailSender;
}
Expand Down
6 changes: 5 additions & 1 deletion odd-platform-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,12 @@ notifications:
# email:
# sender:
# password:
# smtp:
# host:
# port:
# protocol:
# smtp:
# auth:
# starttls:
# notification:
# emails: "[email protected],[email protected]"

Expand Down

0 comments on commit d2172b4

Please sign in to comment.