Source code: org/roller/util/MailUtil.java
1 package org.roller.util;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6
7 import javax.mail.Message;
8 import javax.mail.MessagingException;
9 import javax.mail.Session;
10 import javax.mail.Transport;
11 import javax.mail.internet.InternetAddress;
12 import javax.mail.internet.MimeMessage;
13
14
15 public class MailUtil extends Object {
16
17 private static Log mLogger =
18 LogFactory.getFactory().getInstance(MailUtil.class);
19
20 /**
21 * This method is used to send a Message with a pre-defined
22 * mime-type.
23 *
24 * @param from e-mail address of sender
25 * @param to e-mail address(es) of recipients
26 * @param subject subject of e-mail
27 * @param content the body of the e-mail
28 * @param mimeType type of message, i.e. text/plain or text/html
29 * @throws MessagingException the exception to indicate failure
30 */
31 public static void sendMessage
32 (
33 Session session,
34 String from,
35 String[] to,
36 String[] cc,
37 String subject,
38 String content,
39 String mimeType
40 )
41 throws MessagingException
42 {
43 Message message = new MimeMessage(session);
44
45 // TODO Add configuration for default "from" email address
46 if (! StringUtils.isEmpty(from)) {
47 InternetAddress sentFrom = new InternetAddress(from);
48 message.setFrom(sentFrom);
49 if (mLogger.isDebugEnabled())
50 {
51 mLogger.debug("e-mail from: " + sentFrom);
52 }
53 }
54
55 InternetAddress[] sendTo = new InternetAddress[to.length];
56
57 for (int i = 0; i < to.length; i++)
58 {
59 sendTo[i] = new InternetAddress(to[i]);
60
61 if (mLogger.isDebugEnabled())
62 {
63 mLogger.debug("sending e-mail to: " + to[i]);
64 }
65 }
66 message.setRecipients(Message.RecipientType.TO, sendTo);
67
68 if (cc != null)
69 {
70 InternetAddress[] copyTo = new InternetAddress[cc.length];
71
72 for (int i = 0; i < cc.length; i++)
73 {
74 copyTo[i] = new InternetAddress(cc[i]);
75
76 if (mLogger.isDebugEnabled())
77 {
78 mLogger.debug("copying e-mail to: " + cc[i]);
79 }
80 }
81
82 message.setRecipients(Message.RecipientType.CC, copyTo);
83 }
84
85 message.setSubject((subject == null) ? "(no subject)" : subject);
86 message.setContent(content, mimeType);
87
88 Transport.send(message);
89 }
90
91 /**
92 * This method is used to send a Text Message.
93 *
94 * @param from e-mail address of sender
95 * @param to e-mail addresses of recipients
96 * @param subject subject of e-mail
97 * @param content the body of the e-mail
98 * @throws MessagingException the exception to indicate failure
99 */
100 public static void sendTextMessage
101 (
102 Session session,
103 String from,
104 String[] to,
105 String[] cc,
106 String subject,
107 String content
108 )
109 throws MessagingException
110 {
111 sendMessage(session, from, to, cc, subject, content, "text/plain");
112 }
113
114 /**
115 * This method overrides the sendTextMessage to specify
116 * one receiver and mulitple cc recipients.
117 *
118 * @param from e-mail address of sender
119 * @param to e-mail addresses of recipients
120 * @param subject subject of e-mail
121 * @param content the body of the e-mail
122 * @throws MessagingException the exception to indicate failure
123 */
124 public static void sendTextMessage
125 (
126 Session session,
127 String from,
128 String to,
129 String[] cc,
130 String subject,
131 String content
132 )
133 throws MessagingException
134 {
135 String[] recipient = {to};
136 sendMessage(session, from, recipient, cc, subject, content, "text/plain");
137 }
138
139 /**
140 * This method overrides the sendTextMessage to specify
141 * only one receiver and cc recipients, rather than
142 * an array of recipients.
143 *
144 * @param from e-mail address of sender
145 * @param to e-mail address of recipient
146 * @param cc e-mail address of cc recipient
147 * @param subject subject of e-mail
148 * @param content the body of the e-mail
149 * @throws MessagingException the exception to indicate failure
150 */
151 public static void sendTextMessage
152 (
153 Session session,
154 String from,
155 String to,
156 String cc,
157 String subject,
158 String content
159 )
160 throws MessagingException
161 {
162 String[] recipient = {to};
163 String[] copy = {cc};
164 sendMessage(session, from, recipient, copy, subject, content, "text/plain");
165 }
166
167 /**
168 * This method is used to send a HTML Message
169 *
170 * @param from e-mail address of sender
171 * @param to e-mail address(es) of recipients
172 * @param subject subject of e-mail
173 * @param content the body of the e-mail
174 * @throws MessagingException the exception to indicate failure
175 */
176 public static void sendHTMLMessage
177 (
178 Session session,
179 String from,
180 String[] to,
181 String[] cc,
182 String subject,
183 String content
184 )
185 throws MessagingException
186 {
187 sendMessage(session, from, to, cc, subject, content, "text/html");
188 }
189
190 /**
191 * This method overrides the sendHTMLMessage to specify
192 * only one sender, rather than an array of senders.
193 *
194 * @param from e-mail address of sender
195 * @param to e-mail address of recipients
196 * @param subject subject of e-mail
197 * @param content the body of the e-mail
198 * @throws MessagingException the exception to indicate failure
199 */
200 public static void sendHTMLMessage
201 (
202 Session session,
203 String from,
204 String to,
205 String cc,
206 String subject,
207 String content
208 )
209 throws MessagingException
210 {
211 String[] recipient = {to};
212 String[] copy = {cc};
213 sendMessage(session, from, recipient, copy, subject, content, "text/html");
214 }
215
216 /**
217 * This method overrides the sendHTMLMessage to specify
218 * one receiver and mulitple cc recipients.
219 *
220 * @param from e-mail address of sender
221 * @param to e-mail address of recipient
222 * @param cc e-mail addresses of recipients
223 * @param subject subject of e-mail
224 * @param content the body of the e-mail
225 * @throws MessagingException the exception to indicate failure
226 */
227 public static void sendHTMLMessage
228 (
229 Session session,
230 String from,
231 String to,
232 String[] cc,
233 String subject,
234 String content
235 )
236 throws MessagingException
237 {
238 String[] recipient = {to};
239 sendMessage(session, from, recipient, cc, subject, content, "text/html");
240 }
241 }