1
1
package run .halo .app .notification ;
2
2
3
3
import com .fasterxml .jackson .databind .JsonNode ;
4
- import java .nio .charset .StandardCharsets ;
5
- import java .util .Properties ;
6
4
import java .util .concurrent .atomic .AtomicReference ;
7
- import lombok .Data ;
8
5
import lombok .NonNull ;
9
6
import lombok .RequiredArgsConstructor ;
10
7
import lombok .extern .slf4j .Slf4j ;
11
8
import org .apache .commons .lang3 .StringUtils ;
12
9
import org .springframework .data .util .Pair ;
10
+ import org .springframework .mail .javamail .JavaMailSender ;
13
11
import org .springframework .mail .javamail .JavaMailSenderImpl ;
14
- import org .springframework .mail .javamail .MimeMessageHelper ;
15
12
import org .springframework .mail .javamail .MimeMessagePreparator ;
16
13
import org .springframework .stereotype .Component ;
17
14
import reactor .core .publisher .Mono ;
18
15
import reactor .core .scheduler .Schedulers ;
19
16
import run .halo .app .core .extension .notification .Subscription ;
20
17
import run .halo .app .infra .utils .JsonUtils ;
18
+ import run .halo .app .notification .EmailSenderHelper .EmailSenderConfig ;
21
19
22
20
/**
23
21
* <p>A notifier that can send email.</p>
@@ -34,7 +32,8 @@ public class EmailNotifier implements ReactiveNotifier {
34
32
35
33
private final SubscriberEmailResolver subscriberEmailResolver ;
36
34
private final NotificationTemplateRender notificationTemplateRender ;
37
- private final AtomicReference <Pair <EmailSenderConfig , JavaMailSenderImpl >>
35
+ private final EmailSenderHelper emailSenderHelper ;
36
+ private final AtomicReference <Pair <EmailSenderConfig , JavaMailSender >>
38
37
emailSenderConfigPairRef = new AtomicReference <>();
39
38
40
39
@ Override
@@ -48,7 +47,7 @@ public Mono<Void> notify(NotificationContext context) {
48
47
return Mono .empty ();
49
48
}
50
49
51
- JavaMailSenderImpl javaMailSender = getJavaMailSender (emailSenderConfig );
50
+ JavaMailSender javaMailSender = getJavaMailSender (emailSenderConfig );
52
51
53
52
String recipient = context .getMessage ().getRecipient ();
54
53
var subscriber = new Subscription .Subscriber ();
@@ -83,55 +82,20 @@ public Mono<Void> notify(NotificationContext context) {
83
82
}
84
83
85
84
@ NonNull
86
- private static MimeMessagePreparator getMimeMessagePreparator (String toEmail ,
85
+ private MimeMessagePreparator getMimeMessagePreparator (String toEmail ,
87
86
EmailSenderConfig emailSenderConfig , NotificationContext .MessagePayload payload ) {
88
- return mimeMessage -> {
89
- MimeMessageHelper helper =
90
- new MimeMessageHelper (mimeMessage , true , StandardCharsets .UTF_8 .name ());
91
- helper .setFrom (emailSenderConfig .getUsername (), emailSenderConfig .getDisplayName ());
92
-
93
- helper .setSubject (payload .getTitle ());
94
- helper .setText (payload .getRawBody (), payload .getHtmlBody ());
95
- helper .setTo (toEmail );
96
- };
97
- }
98
-
99
- @ NonNull
100
- private static JavaMailSenderImpl createJavaMailSender (EmailSenderConfig emailSenderConfig ) {
101
- JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl ();
102
- javaMailSender .setHost (emailSenderConfig .getHost ());
103
- javaMailSender .setPort (emailSenderConfig .getPort ());
104
- javaMailSender .setUsername (emailSenderConfig .getUsername ());
105
- javaMailSender .setPassword (emailSenderConfig .getPassword ());
106
-
107
- Properties props = javaMailSender .getJavaMailProperties ();
108
- props .put ("mail.transport.protocol" , "smtp" );
109
- props .put ("mail.smtp.auth" , "true" );
110
- if ("SSL" .equals (emailSenderConfig .getEncryption ())) {
111
- props .put ("mail.smtp.ssl.enable" , "true" );
112
- }
113
-
114
- if ("TLS" .equals (emailSenderConfig .getEncryption ())) {
115
- props .put ("mail.smtp.starttls.enable" , "true" );
116
- }
117
-
118
- if ("NONE" .equals (emailSenderConfig .getEncryption ())) {
119
- props .put ("mail.smtp.ssl.enable" , "false" );
120
- props .put ("mail.smtp.starttls.enable" , "false" );
121
- }
122
-
123
- if (log .isDebugEnabled ()) {
124
- props .put ("mail.debug" , "true" );
125
- }
126
- return javaMailSender ;
87
+ return emailSenderHelper .createMimeMessagePreparator (emailSenderConfig , toEmail ,
88
+ payload .getTitle (),
89
+ payload .getRawBody (), payload .getHtmlBody ());
127
90
}
128
91
129
- JavaMailSenderImpl getJavaMailSender (EmailSenderConfig emailSenderConfig ) {
92
+ JavaMailSender getJavaMailSender (EmailSenderConfig emailSenderConfig ) {
130
93
return emailSenderConfigPairRef .updateAndGet (pair -> {
131
94
if (pair != null && pair .getFirst ().equals (emailSenderConfig )) {
132
95
return pair ;
133
96
}
134
- return Pair .of (emailSenderConfig , createJavaMailSender (emailSenderConfig ));
97
+ return Pair .of (emailSenderConfig ,
98
+ emailSenderHelper .createJavaMailSender (emailSenderConfig ));
135
99
}).getSecond ();
136
100
}
137
101
@@ -156,24 +120,4 @@ Mono<String> appendHtmlBodyFooter(ReasonAttributes attributes) {
156
120
</div>
157
121
""" , attributes );
158
122
}
159
-
160
- @ Data
161
- static class EmailSenderConfig {
162
- private boolean enable ;
163
- private String displayName ;
164
- private String username ;
165
- private String password ;
166
- private String host ;
167
- private Integer port ;
168
- private String encryption ;
169
-
170
- /**
171
- * Gets email display name.
172
- *
173
- * @return display name if not blank, otherwise username.
174
- */
175
- public String getDisplayName () {
176
- return StringUtils .defaultIfBlank (displayName , username );
177
- }
178
- }
179
123
}
0 commit comments