Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1697 - email notification improvements #1698

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading