1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.core.mail;
17
18 import org.kuali.rice.core.api.mail.EmailBcList;
19 import org.kuali.rice.core.api.mail.EmailBody;
20 import org.kuali.rice.core.api.mail.EmailCcList;
21 import org.kuali.rice.core.api.mail.EmailFrom;
22 import org.kuali.rice.core.api.mail.EmailSubject;
23 import org.kuali.rice.core.api.mail.EmailTo;
24 import org.kuali.rice.core.api.mail.EmailToList;
25 import org.kuali.rice.core.api.mail.MailMessage;
26 import org.kuali.rice.core.api.mail.Mailer;
27 import org.springframework.mail.MailException;
28 import org.springframework.mail.SimpleMailMessage;
29 import org.springframework.mail.javamail.JavaMailSenderImpl;
30
31 import javax.activation.DataHandler;
32 import javax.mail.Address;
33 import javax.mail.Message;
34 import javax.mail.MessagingException;
35 import javax.mail.internet.AddressException;
36 import javax.mail.internet.InternetAddress;
37 import javax.mail.internet.MimeMessage;
38 import javax.mail.util.ByteArrayDataSource;
39 import java.io.IOException;
40
41
42
43
44
45
46
47 public class MailerImpl implements Mailer {
48
49 protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MailerImpl.class);
50
51 private JavaMailSenderImpl mailSender;
52
53
54
55
56 public void setMailSender(JavaMailSenderImpl mailSender) {
57 this.mailSender = mailSender;
58 }
59
60
61
62
63
64
65
66
67 @Override
68 @SuppressWarnings("unchecked")
69 public void sendEmail(MailMessage message) throws MessagingException {
70
71
72 SimpleMailMessage smm = new SimpleMailMessage();
73 smm.setTo( (String[])message.getToAddresses().toArray(new String[message.getToAddresses().size()]) );
74 smm.setBcc( (String[])message.getBccAddresses().toArray(new String[message.getBccAddresses().size()]) );
75 smm.setCc( (String[])message.getCcAddresses().toArray(new String[message.getCcAddresses().size()]) );
76 smm.setSubject(message.getSubject());
77 smm.setText(message.getMessage());
78 smm.setFrom(message.getFromAddress());
79
80 try {
81 if ( LOG.isDebugEnabled() ) {
82 LOG.debug( "sendEmail() - Sending message: " + smm.toString() );
83 }
84 mailSender.send(smm);
85 }
86 catch (Exception e) {
87 LOG.error("sendEmail() - Error sending email.", e);
88 throw new RuntimeException(e);
89 }
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 @Override
106 public void sendEmail(EmailFrom from, EmailTo to, EmailSubject subject, EmailBody body, boolean htmlMessage) {
107 if (to.getToAddress() == null) {
108 LOG.warn("No To address specified. Refraining from sending mail.");
109 return;
110 }
111 try {
112 Address[] singleRecipient = {new InternetAddress(to.getToAddress())};
113 sendMessage(from.getFromAddress(),
114 singleRecipient,
115 subject.getSubject(),
116 body.getBody(),
117 null,
118 null,
119 htmlMessage);
120 } catch (Exception e) {
121 LOG.error("sendEmail(): ", e);
122 throw new RuntimeException(e);
123 }
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 @Override
143 public void sendEmail(EmailFrom from, EmailToList to, EmailSubject subject, EmailBody body, EmailCcList cc,
144 EmailBcList bc, boolean htmlMessage) {
145 if (to.getToAddresses().isEmpty()) {
146 LOG.error("List of To addresses must contain at least one entry. Refraining from sending mail.");
147 return;
148 }
149 try {
150 sendMessage(from.getFromAddress(),
151 to.getToAddressesAsAddressArray(),
152 subject.getSubject(),
153 body.getBody(),
154 (cc == null ? null : cc.getToAddressesAsAddressArray()),
155 (bc == null ? null : bc.getToAddressesAsAddressArray()),
156 htmlMessage);
157 } catch (Exception e) {
158 LOG.error("sendEmail(): ", e);
159 throw new RuntimeException(e);
160 }
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 protected void sendMessage(String from, Address[] to, String subject, String messageBody, Address[] cc, Address[] bcc, boolean htmlMessage) throws AddressException, MessagingException, MailException {
180 MimeMessage message = mailSender.createMimeMessage();
181
182
183 message.setFrom(new InternetAddress(from));
184
185
186 if (to != null && to.length > 0) {
187 message.addRecipients(Message.RecipientType.TO, to);
188 } else {
189 LOG.error("No recipients indicated.");
190 }
191
192
193 if (cc != null && cc.length > 0) {
194 message.addRecipients(Message.RecipientType.CC, cc);
195 }
196
197
198 if (bcc != null && bcc.length > 0) {
199 message.addRecipients(Message.RecipientType.BCC, bcc);
200 }
201
202
203 message.setSubject(subject);
204 if (subject == null || "".equals(subject)) {
205 LOG.warn("Empty subject being sent.");
206 }
207
208
209 if (htmlMessage) {
210 prepareHtmlMessage(messageBody, message);
211 } else {
212 message.setText(messageBody);
213 if (messageBody == null || "".equals(messageBody)) {
214 LOG.warn("Empty message body being sent.");
215 }
216 }
217
218
219 try {
220 mailSender.send(message);
221 }
222 catch (Exception e) {
223 LOG.error("sendMessage(): ", e);
224 throw new RuntimeException(e);
225 }
226 }
227
228 protected void prepareHtmlMessage(String messageText, Message message) throws MessagingException {
229 try {
230 message.setDataHandler(new DataHandler(new ByteArrayDataSource(messageText, "text/html")));
231 } catch (IOException e) {
232 LOG.warn(e.getMessage());
233 throw new RuntimeException(e);
234 }
235 }
236 }