1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.kew.mail;
18
19 import java.util.Properties;
20
21 import javax.activation.DataHandler;
22 import javax.mail.Address;
23 import javax.mail.Authenticator;
24 import javax.mail.Message;
25 import javax.mail.MessagingException;
26 import javax.mail.NoSuchProviderException;
27 import javax.mail.PasswordAuthentication;
28 import javax.mail.Session;
29 import javax.mail.Transport;
30 import javax.mail.internet.AddressException;
31 import javax.mail.internet.InternetAddress;
32 import javax.mail.internet.MimeMessage;
33
34 import org.kuali.rice.kew.util.ByteArrayDataSource;
35
36
37
38
39
40
41
42 public class Mailer {
43
44 protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
45
46 private Properties configProperties;
47 private Authenticator authenticator;
48
49 private Session currentSession;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public Mailer(Properties configProperties, Authenticator authenticator) {
66 this.configProperties = configProperties;
67 this.authenticator = authenticator;
68 }
69
70 public Mailer(Properties configProperties) {
71 this(configProperties, null);
72 }
73
74 public Mailer(Properties configProperties, String username, String password) {
75 this(configProperties, new SimpleAuthenticator(username, password));
76 }
77
78
79
80
81
82
83
84
85
86 public void setConfig(Properties configProperties) {
87 this.configProperties = configProperties;
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 public void sendMessage(String sender, Address[] recipients, String subject, String messageBody, Address[] ccRecipients, Address[] bccRecipients, boolean htmlMessage) throws AddressException, MessagingException {
108 Session session = getCurrentSession();
109 session.setDebug(LOG.isDebugEnabled());
110 Message message = new MimeMessage(session);
111
112
113 message.setFrom(new InternetAddress(sender));
114
115
116 if (recipients != null && recipients.length > 0) {
117 message.addRecipients(Message.RecipientType.TO, recipients);
118 } else {
119 LOG.warn("No recipients indicated");
120 }
121
122
123 if (ccRecipients != null && ccRecipients.length > 0) {
124 message.addRecipients(Message.RecipientType.CC, ccRecipients);
125 }
126
127
128 if (bccRecipients != null && bccRecipients.length > 0) {
129 message.addRecipients(Message.RecipientType.BCC, bccRecipients);
130 }
131
132
133 message.setSubject(subject);
134 if (subject == null || "".equals(subject)) {
135 LOG.warn("Empty subject being sent");
136 }
137
138
139
140 if (htmlMessage) {
141 prepareHtmlMessage(messageBody, message);
142 } else {
143 message.setText(messageBody);
144 if (messageBody == null || "".equals(messageBody)) {
145 LOG.warn("Empty message body being sent.");
146 }
147 }
148
149
150 Transport.send(message);
151
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 public void sendMessage(String sender, String recipient, String subject, String messageBody, boolean htmlMessage) throws AddressException, MessagingException {
168 final Address[] NO_RECIPIENTS = null;
169 Address[] recipients = { new InternetAddress(recipient) };
170 sendMessage(sender, recipients, subject, messageBody, NO_RECIPIENTS, NO_RECIPIENTS, htmlMessage);
171 }
172
173
174
175
176 public Properties getConfig() {
177 return configProperties;
178 }
179
180 public Authenticator getAuthenticator() {
181 return authenticator;
182 }
183
184
185
186
187
188
189
190
191
192 public Session getCurrentSession() throws NoSuchProviderException {
193 if (this.currentSession == null || !this.currentSession.getTransport().isConnected()) {
194 this.currentSession = Session.getInstance(configProperties, authenticator);
195 }
196 return currentSession;
197 }
198
199 private void prepareHtmlMessage(String messageText, Message message) throws MessagingException {
200 message.setDataHandler(new DataHandler(new ByteArrayDataSource(messageText, "text/html")));
201 }
202
203
204
205
206
207 private static class SimpleAuthenticator extends javax.mail.Authenticator {
208
209 private final PasswordAuthentication passwordAuthentication;
210
211 private SimpleAuthenticator(String username, String password) {
212 this.passwordAuthentication = new PasswordAuthentication(username, password);
213 }
214
215 public PasswordAuthentication getPasswordAuthentication() {
216 return passwordAuthentication;
217 }
218
219 }
220
221 }