1
2
3
4
5
6 package pt.digitalis.dif.utils.mail;
7
8 import java.util.Arrays;
9 import java.util.Date;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.Properties;
15
16 import javax.activation.DataHandler;
17 import javax.activation.FileDataSource;
18 import javax.mail.Message;
19 import javax.mail.MessagingException;
20 import javax.mail.Multipart;
21 import javax.mail.Session;
22 import javax.mail.Transport;
23 import javax.mail.internet.AddressException;
24 import javax.mail.internet.InternetAddress;
25 import javax.mail.internet.MimeBodyPart;
26 import javax.mail.internet.MimeMessage;
27 import javax.mail.internet.MimeMultipart;
28
29 import pt.digitalis.dif.ioc.DIFIoCRegistry;
30 import pt.digitalis.dif.utils.logging.DIFLogger;
31 import pt.digitalis.dif.utils.logging.IErrorLogManager;
32 import pt.digitalis.utils.common.StringUtils;
33
34
35
36
37
38
39
40 public class MailSender {
41
42
43
44
45 protected static MailSender instance;
46
47
48
49
50
51
52 public static MailSender getInstance()
53 {
54
55 if (instance == null)
56 {
57 IMailConfiguration mailConfiguration = MailConfiguration.getInstance();
58
59 instance = new MailSender(mailConfiguration);
60 }
61
62 return instance;
63 }
64
65
66
67
68
69
70
71 public static void setInstance(MailSender instance)
72 {
73 MailSender.instance = instance;
74 }
75
76
77 private IMailConfiguration configuration = null;
78
79
80 IErrorLogManager errorLogManager = DIFIoCRegistry.getRegistry().getImplementation(IErrorLogManager.class);
81
82
83
84
85
86
87
88 public MailSender(IMailConfiguration configuration)
89 {
90 this.configuration = configuration;
91 }
92
93
94
95
96
97
98 public IMailConfiguration getConfiguration()
99 {
100 return configuration;
101 }
102
103
104
105
106
107
108 private Session getMailSession()
109 {
110 Properties props = new Properties();
111 props.setProperty("mail.transport.protocol", "smtp");
112 props.setProperty("mail.store.protocol", "pop3");
113 props.setProperty("mail.host", configuration.getMailServer());
114
115 if (configuration.getMailServerUsername() != null && !"".equals(configuration.getMailServerUsername()))
116 props.setProperty("mail.user", configuration.getMailServerUsername());
117
118 if (configuration.getMailServerPassword() != null && !"".equals(configuration.getMailServerPassword()))
119 props.setProperty("mail.smtp.auth", "true");
120 else
121 props.setProperty("mail.smtp.auth", "false");
122
123 props.setProperty("mail.from", configuration.getDefaultFromAddress());
124
125 if (configuration.getMailServerPort() != 0)
126 props.setProperty("mail.smtp.port", configuration.getMailServerPort() + "");
127
128 props.put("mail.debug", configuration.getDebugEnabled());
129
130 props.put("mail.smtp.ssl.enable", configuration.getUseSSL());
131
132 if (configuration.getOriginalConfs() != null)
133 {
134 props.putAll(configuration.getOriginalConfs());
135 }
136
137 DIFLogger.getLogger().debug(props);
138
139 Session mailSession = Session.getDefaultInstance(props);
140
141 return mailSession;
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 public boolean sendEmail(MailType type, String from, String to, String cc, String bcc, String subject, String body,
172 List<String> attachments, Map<String, String> images) throws AddressException, MessagingException
173 {
174 return sendEmail(type, from, to, cc, bcc, subject, body, attachments, images, false);
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 public boolean sendEmail(MailType type, String from, String to, String cc, String bcc, String subject, String body,
204 List<String> attachments, Map<String, String> images, boolean disableErrorLog) throws MessagingException
205 {
206 try
207 {
208
209 Session session = getMailSession();
210
211
212 Message message = new MimeMessage(session);
213
214 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
215
216 message.setSentDate(new Date());
217 message.setSubject(subject);
218
219 if (from != null)
220 message.setFrom(InternetAddress.parse(from)[0]);
221 else
222 message.setFrom(InternetAddress.parse(configuration.getDefaultFromAddress())[0]);
223
224 if (cc != null)
225 message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
226 if (bcc != null)
227 message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
228
229 if (attachments != null || images != null)
230 {
231 Multipart multipart = new MimeMultipart();
232
233
234 MimeBodyPart messagePart = new MimeBodyPart();
235
236 if (MailType.HTML.equals(type))
237 messagePart.setContent(body,
238 "text/html"
239 + (StringUtils.isBlank(configuration.getEncoding()) ? "" : "; charset="
240 + configuration.getEncoding()));
241 else
242 messagePart.setContent(body,
243 "text/plain"
244 + (StringUtils.isBlank(configuration.getEncoding()) ? "" : "; charset="
245 + configuration.getEncoding()));
246 messagePart.setText(body);
247 multipart.addBodyPart(messagePart);
248
249 if (attachments != null)
250 {
251
252 for (String attach: attachments)
253 {
254 MimeBodyPart attachmentPart = new MimeBodyPart();
255 attachmentPart.setDataHandler(new DataHandler(new FileDataSource(attach)));
256
257
258 multipart.addBodyPart(attachmentPart);
259 }
260 }
261
262 if (images != null)
263 {
264
265 for (Entry<String, String> image: images.entrySet())
266 {
267 MimeBodyPart embededImagePart = new MimeBodyPart();
268 embededImagePart.setDataHandler(new DataHandler(new FileDataSource(image.getValue())));
269 embededImagePart.setHeader("Content-ID", "<" + image.getKey() + ">");
270
271 multipart.addBodyPart(embededImagePart);
272 }
273 }
274
275 message.setContent(multipart);
276 }
277 else
278 {
279 if (MailType.HTML.equals(type))
280 {
281 MimeMultipart multipart = new MimeMultipart();
282 MimeBodyPart messagePart;
283
284
285 messagePart = new MimeBodyPart();
286 messagePart.setContent(body,
287 "text/html"
288 + (StringUtils.isBlank(configuration.getEncoding()) ? "" : "; charset="
289 + configuration.getEncoding()));
290 multipart.addBodyPart(messagePart);
291
292
293
294
295
296
297
298
299
300
301
302
303 message.setContent(multipart);
304 }
305 else
306 {
307 message.setContent(body,
308 "text/plain"
309 + (StringUtils.isBlank(configuration.getEncoding()) ? "" : "; charset="
310 + configuration.getEncoding()));
311 message.setText(body);
312 }
313 }
314
315
316 Transport tr = session.getTransport("smtp");
317 tr.connect(configuration.getMailServer(), configuration.getMailServerUsername(),
318 configuration.getMailServerPassword());
319 message.saveChanges();
320 tr.sendMessage(message, message.getAllRecipients());
321 tr.close();
322
323 }
324 catch (AddressException e)
325 {
326 errorLogManager.logError(MailSender.class.getSimpleName(), "sendMail", e.getMessage());
327 throw e;
328 }
329 catch (MessagingException e)
330 {
331 errorLogManager.logError(MailSender.class.getSimpleName(), "sendMail", e.getMessage());
332 throw e;
333 }
334
335 return false;
336 }
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357 public boolean sendHTMLEmail(String to, String cc, String bcc, String subject, String body)
358 throws AddressException, MessagingException
359 {
360 return sendHTMLEmail(to, cc, bcc, subject, body, (List<String>) null, (Map<String, String>) null);
361 }
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386 public boolean sendHTMLEmail(String to, String cc, String bcc, String subject, String body,
387 List<String> attachments, Map<String, String> images) throws AddressException, MessagingException
388 {
389 return sendEmail(MailType.HTML, configuration.getDefaultFromAddress(), to, cc, bcc, subject, body, attachments,
390 images);
391 }
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416 public boolean sendHTMLEmail(String to, String cc, String bcc, String subject, String body, String attachments,
417 String images) throws AddressException, MessagingException
418 {
419 List<String> attachList = null;
420 Map<String, String> imageMap = null;
421
422 if (attachments != null)
423 attachList = Arrays.asList(attachments.split(","));
424
425 if (images != null)
426 {
427 String[] imagePairs = images.split(";");
428
429 for (String imagePair: imagePairs)
430 {
431 String[] info = imagePair.split("=");
432 if (info.length == 2)
433 {
434 if (imageMap == null)
435 {
436 imageMap = new HashMap<String, String>();
437 }
438
439 imageMap.put(info[0], info[1]);
440 }
441 }
442 }
443
444 return sendHTMLEmail(to, cc, bcc, subject, body, attachList, imageMap);
445 }
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468 public boolean sendHTMLEmailWithAttachements(String to, String cc, String bcc, String subject, String body,
469 List<String> attachments) throws AddressException, MessagingException
470 {
471 return sendHTMLEmail(to, cc, bcc, subject, body, attachments, null);
472 }
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495 public boolean sendHTMLEmailWithAttachements(String to, String cc, String bcc, String subject, String body,
496 String attachments) throws AddressException, MessagingException
497 {
498 List<String> attachList = null;
499
500 if (attachments != null)
501 attachList = Arrays.asList(attachments.split(","));
502
503 return sendHTMLEmailWithAttachements(to, cc, bcc, subject, body, attachList);
504 }
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527 public boolean sendHTMLEmailWithImages(String to, String cc, String bcc, String subject, String body,
528 Map<String, String> images) throws AddressException, MessagingException
529 {
530 return sendHTMLEmail(to, cc, bcc, subject, body, null, images);
531 }
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554 public boolean sendHTMLEmailWithImages(String to, String cc, String bcc, String subject, String body, String images)
555 throws AddressException, MessagingException
556 {
557 List<String> imageList = null;
558
559 if (images != null)
560 imageList = Arrays.asList(images.split(","));
561
562 return sendHTMLEmailWithAttachements(to, cc, bcc, subject, body, imageList);
563 }
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584 public boolean sendTextEmail(String to, String cc, String bcc, String subject, String body)
585 throws AddressException, MessagingException
586 {
587 return sendTextEmail(to, cc, bcc, subject, body, (List<String>) null);
588 }
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611 public boolean sendTextEmail(String to, String cc, String bcc, String subject, String body, List<String> attachments)
612 throws AddressException, MessagingException
613 {
614 return sendEmail(MailType.PLAIN_TEXT, configuration.getDefaultFromAddress(), to, cc, bcc, subject, body,
615 attachments, null);
616 }
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639 public boolean sendTextEmail(String to, String cc, String bcc, String subject, String body, String attachments)
640 throws AddressException, MessagingException
641 {
642 List<String> attachList = null;
643
644 if (attachments != null)
645 attachList = Arrays.asList(attachments.split(","));
646
647 return sendTextEmail(to, cc, bcc, subject, body, attachList);
648 }
649
650 }