1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.kernel.mail;
23
24 import org.apache.commons.lang3.BooleanUtils;
25 import org.apache.commons.lang3.StringUtils;
26 import org.osgi.service.cm.ConfigurationException;
27 import org.osgi.service.cm.ManagedService;
28 import org.osgi.service.component.annotations.Component;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.util.Dictionary;
33
34 import javax.mail.Message.RecipientType;
35 import javax.mail.MessagingException;
36 import javax.mail.internet.InternetAddress;
37 import javax.mail.internet.MimeBodyPart;
38 import javax.mail.internet.MimeMessage;
39 import javax.mail.internet.MimeMultipart;
40
41
42
43
44 @Component(
45 immediate = true,
46 service = { ManagedService.class,SmtpService.class },
47 property = {
48 "service.description=SMTP Service"
49 }
50 )
51 public class SmtpService extends BaseSmtpService implements ManagedService {
52
53
54 private static final Logger logger = LoggerFactory.getLogger(SmtpService.class);
55
56
57 private static final String OPT_MAIL_TEST = "mail.test";
58
59
60 private static final String OPT_MAIL_MODE = "mail.mode";
61
62
63
64
65 private static final String SPLIT_PATTERN = "[\\s,]+";
66
67
68 private static final String TEXT_PLAIN = "text/plain; charset=UTF-8";
69
70
71 private static final String TEXT_HTML = "text/html; charset=UTF-8";
72
73
74
75
76
77
78
79
80
81 @Override
82 public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
83
84
85 String optMode = StringUtils.trimToNull((String) properties.get(OPT_MAIL_MODE));
86 if (optMode != null) {
87 try {
88 Mode mode = Mode.valueOf(optMode);
89 setProductionMode(Mode.production.equals(mode));
90 } catch (Exception e) {
91 logger.error("Error parsing smtp service mode '{}': {}", optMode, e.getMessage());
92 throw new ConfigurationException(OPT_MAIL_MODE, e.getMessage());
93 }
94 }
95 logger.info("Smtp service is in {} mode", isProductionMode() ? "production" : "test");
96
97
98 String optMailTransport = StringUtils.trimToNull((String) properties.get(OPT_MAIL_TRANSPORT));
99 if (StringUtils.isNotBlank(optMailTransport))
100 setMailTransport(optMailTransport);
101
102
103 String propName = OPT_MAIL_PREFIX + mailTransport + OPT_MAIL_HOST_SUFFIX;
104 String mailHost = (String) properties.get(propName);
105 if (StringUtils.isBlank(mailHost))
106 throw new ConfigurationException(propName, "is not set");
107 setHost(mailHost);
108
109
110 propName = OPT_MAIL_PREFIX + mailTransport + OPT_MAIL_PORT_SUFFIX;
111 String mailPort = (String) properties.get(propName);
112 if (StringUtils.isNotBlank(mailPort))
113 setPort(Integer.parseInt(mailPort));
114
115
116 propName = OPT_MAIL_PREFIX + mailTransport + OPT_MAIL_TLS_ENABLE_SUFFIX;
117 setSsl(BooleanUtils.toBoolean((String) properties.get(propName)));
118
119
120 String mailUser = (String) properties.get(OPT_MAIL_USER);
121 if (StringUtils.isNotBlank(mailUser))
122 setUser(mailUser);
123
124
125 String mailPassword = (String) properties.get(OPT_MAIL_PASSWORD);
126 if (StringUtils.isNotBlank(mailPassword))
127 setPassword(mailPassword);
128
129
130 String mailFrom = (String) properties.get(OPT_MAIL_FROM);
131 if (StringUtils.isNotBlank(mailFrom))
132 setSender(mailFrom);
133
134
135 setDebug(BooleanUtils.toBoolean((String) properties.get(OPT_MAIL_DEBUG)));
136
137 configure();
138
139
140 String mailTest = StringUtils.trimToNull((String) properties.get(OPT_MAIL_TEST));
141 if (mailTest != null && Boolean.parseBoolean(mailTest)) {
142 logger.info("Sending test message to {}", mailFrom);
143 try {
144 sendTestMessage(mailFrom);
145 } catch (MessagingException e) {
146 logger.error("Error sending test message to " + mailFrom + ": " + e.getMessage());
147 while (e.getNextException() != null) {
148 Exception ne = e.getNextException();
149 logger.error("Error sending test message to " + mailFrom + ": " + ne.getMessage());
150 if (ne instanceof MessagingException)
151 e = (MessagingException) ne;
152 else
153 break;
154 }
155 throw new ConfigurationException(OPT_MAIL_PREFIX + mailTransport + OPT_MAIL_HOST_SUFFIX,
156 "Failed to send test message to " + mailFrom);
157 }
158 }
159
160 }
161
162
163
164
165
166
167
168 private void sendTestMessage(String recipient) throws MessagingException {
169 send(recipient, "Test from Opencast", "Hello world");
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184 public void send(String to, String subject, String bodyText) throws MessagingException {
185 send(to, subject, bodyText, null);
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 public void send(String to, String subject, String bodyText, String bodyHTML) throws MessagingException {
203 send(to, null, null, subject, bodyText, bodyHTML);
204 }
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 public void send(String to, String cc, String bcc, String subject, String bodyText) throws MessagingException {
223 send(to, cc, bcc, subject, bodyText, null);
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244 public void send(String to, String cc, String bcc, String subject, String bodyText, String bodyHTML) throws MessagingException {
245 String[] toArray = null;
246 String[] ccArray = null;
247 String[] bccArray = null;
248
249 if (to != null)
250 toArray = to.trim().split(SPLIT_PATTERN, 0);
251
252 if (cc != null)
253 ccArray = cc.trim().split(SPLIT_PATTERN, 0);
254
255 if (bcc != null)
256 bccArray = bcc.trim().split(SPLIT_PATTERN, 0);
257
258 send(toArray, ccArray, bccArray, subject, bodyText, bodyHTML);
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277 public void send(String[] to, String[] cc, String[] bcc, String subject, String bodyText) throws MessagingException {
278 send(to, cc, bcc, subject, bodyText, null);
279 }
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301 public void send(String[] to, String[] cc, String[] bcc, String subject, String bodyText, String bodyHTML) throws MessagingException {
302 if (bodyText == null && bodyHTML == null) {
303 throw new IllegalArgumentException("bodyText and bodyHTML cannot both be empty");
304 }
305
306 MimeMessage message = createMessage();
307 if (StringUtils.isNotBlank(getSender())) {
308 message.addFrom(new InternetAddress[] { new InternetAddress(getSender()) });
309 }
310 addRecipients(message, RecipientType.TO, to);
311 addRecipients(message, RecipientType.CC, cc);
312 addRecipients(message, RecipientType.BCC, bcc);
313 message.setSubject(subject);
314
315 MimeMultipart mp = new MimeMultipart("alternative");
316
317 if (bodyText != null) {
318 MimeBodyPart part = new MimeBodyPart();
319 part.setContent(bodyText, TEXT_PLAIN);
320 mp.addBodyPart(part);
321 }
322 if (bodyHTML != null) {
323 MimeBodyPart part = new MimeBodyPart();
324 part.setContent(bodyHTML, TEXT_HTML);
325 mp.addBodyPart(part);
326 }
327 message.setContent(mp);
328 message.saveChanges();
329 send(message);
330 }
331
332
333
334
335
336
337
338
339
340
341
342 private static void addRecipients(MimeMessage message, RecipientType type, String... strAddresses)
343 throws MessagingException {
344 if (strAddresses != null) {
345 InternetAddress[] addresses = new InternetAddress[strAddresses.length];
346 for (int i = 0; i < strAddresses.length; i++)
347 addresses[i] = new InternetAddress(strAddresses[i]);
348 message.addRecipients(type, addresses);
349 }
350 }
351
352 }