25
25
26
26
@ Configuration
27
27
@ ConditionalOnNotifications
28
- @ EnableConfigurationProperties (NotificationsProperties .class )
28
+ @ EnableConfigurationProperties ({ NotificationsProperties .class , EmailSenderProperties . class } )
29
29
public class NotificationConfiguration {
30
30
31
31
@ Bean
@@ -35,34 +35,38 @@ public HttpClient httpClient() {
35
35
36
36
@ Bean
37
37
@ 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 ())) {
43
40
throw new IllegalArgumentException ("senderEmail is empty" );
44
41
}
45
42
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" );
48
45
}
49
46
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" );
52
49
}
53
50
54
51
final JavaMailSenderImpl mailSender = new JavaMailSenderImpl ();
55
52
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
+ }
60
60
61
61
final Properties props = mailSender .getJavaMailProperties ();
62
62
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
+ }
66
70
67
71
return mailSender ;
68
72
}
0 commit comments