1
2
3
4
5
6
7
8
9 package org.astrogrid.portal.cocoon.messaging;
10 import java.util.Date;
11
12 import javax.mail.Message;
13 import javax.mail.MessagingException;
14 import javax.mail.Session;
15 import javax.mail.Transport;
16 import javax.mail.internet.InternetAddress;
17 import javax.mail.internet.MimeMessage;
18 /***
19 * Messenger that dispatches emails
20 *
21 * @author jdt
22 */
23 public final class EmailMessenger implements Messenger {
24 /***
25 * List of recipients, comma separated
26 */
27 private String recipients;
28 /***
29 * Mail session
30 */
31 private Session session;
32 /***
33 * Constructor - used by EmailMessengerFactory
34 * @param session an email session
35 * @param recipients comma separated list of recipients
36 *
37 */
38 EmailMessenger(final Session session, final String recipients) {
39 this.recipients=recipients;
40 this.session=session;
41 }
42 /***
43 * Does the donkey work of sending an email
44 *
45 * @param subject subject line
46 * @param message what you want to say
47 * @throws MessengerException if the email fails
48 */
49 public void sendMessage(final String subject, final String message)
50 throws MessengerException {
51 try {
52 final Message mess = new MimeMessage(session);
53 mess.setFrom(InternetAddress.getLocalAddress(session));
54 mess.setRecipients(
55 Message.RecipientType.TO,
56 InternetAddress.parse(recipients));
57 mess.setSubject(subject);
58 mess.setSentDate(new Date());
59 mess.setHeader("X-Mailer", "Maven Auto Build");
60 mess.setContent(message, "text/plain");
61 Transport.send(mess);
62 } catch (MessagingException me){
63 throw new MessengerException("Problem sending message", me);
64 }
65 }
66
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83