From d2172b4e6744601f5cc4b9012f9ec18919f55fda Mon Sep 17 00:00:00 2001 From: Vladysl <45620393+Vladysl@users.noreply.github.com> Date: Wed, 28 Aug 2024 16:55:52 +0300 Subject: [PATCH] 1697 - email notification improvements (#1698) --- .../config/EmailSenderProperties.java | 21 ++++++++++ .../config/NotificationConfiguration.java | 38 ++++++++++--------- .../src/main/resources/application.yml | 6 ++- 3 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/notification/config/EmailSenderProperties.java diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/notification/config/EmailSenderProperties.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/notification/config/EmailSenderProperties.java new file mode 100644 index 000000000..7c1619172 --- /dev/null +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/notification/config/EmailSenderProperties.java @@ -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; + } +} diff --git a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/notification/config/NotificationConfiguration.java b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/notification/config/NotificationConfiguration.java index c5351e22e..83f04285e 100644 --- a/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/notification/config/NotificationConfiguration.java +++ b/odd-platform-api/src/main/java/org/opendatadiscovery/oddplatform/notification/config/NotificationConfiguration.java @@ -25,7 +25,7 @@ @Configuration @ConditionalOnNotifications -@EnableConfigurationProperties(NotificationsProperties.class) +@EnableConfigurationProperties({NotificationsProperties.class, EmailSenderProperties.class}) public class NotificationConfiguration { @Bean @@ -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; } diff --git a/odd-platform-api/src/main/resources/application.yml b/odd-platform-api/src/main/resources/application.yml index 717a2de44..834816aa2 100644 --- a/odd-platform-api/src/main/resources/application.yml +++ b/odd-platform-api/src/main/resources/application.yml @@ -185,8 +185,12 @@ notifications: # email: # sender: # password: -# smtp: +# host: # port: +# protocol: +# smtp: +# auth: +# starttls: # notification: # emails: "yourFirst@gmail.com,yourSecond@gmail.com"