2525
2626@ Configuration
2727@ ConditionalOnNotifications
28- @ EnableConfigurationProperties (NotificationsProperties .class )
28+ @ EnableConfigurationProperties ({ NotificationsProperties .class , EmailSenderProperties . class } )
2929public 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 }
0 commit comments