KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > util > MailUtil


1 package org.roller.util;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
6 import javax.mail.Message JavaDoc;
7 import javax.mail.MessagingException JavaDoc;
8 import javax.mail.SendFailedException JavaDoc;
9 import javax.mail.Session JavaDoc;
10 import javax.mail.Transport JavaDoc;
11 import javax.mail.Address JavaDoc;
12 import javax.mail.internet.InternetAddress JavaDoc;
13 import javax.mail.internet.MimeMessage JavaDoc;
14
15
16 public class MailUtil extends Object JavaDoc {
17    
18     private static Log mLogger =
19         LogFactory.getFactory().getInstance(MailUtil.class);
20
21     // agangolli: Incorporated suggested changes from Ken Blackler.
22

23     /**
24      * This method is used to send a Message with a pre-defined
25      * mime-type.
26      *
27      * @param from e-mail address of sender
28      * @param to e-mail address(es) of recipients
29      * @param subject subject of e-mail
30      * @param content the body of the e-mail
31      * @param mimeType type of message, i.e. text/plain or text/html
32      * @throws MessagingException the exception to indicate failure
33      */

34     public static void sendMessage
35     (
36         Session JavaDoc session,
37         String JavaDoc from,
38         String JavaDoc[] to,
39         String JavaDoc[] cc,
40         String JavaDoc[] bcc,
41         String JavaDoc subject,
42         String JavaDoc content,
43         String JavaDoc mimeType
44     )
45     throws MessagingException JavaDoc
46     {
47         Message JavaDoc message = new MimeMessage JavaDoc(session);
48
49         // n.b. any default from address is expected to be determined by caller.
50
if (! StringUtils.isEmpty(from)) {
51             InternetAddress JavaDoc sentFrom = new InternetAddress JavaDoc(from);
52             message.setFrom(sentFrom);
53             if (mLogger.isDebugEnabled()) mLogger.debug("e-mail from: " + sentFrom);
54         }
55
56         if (to!=null)
57         {
58             InternetAddress JavaDoc[] sendTo = new InternetAddress JavaDoc[to.length];
59     
60             for (int i = 0; i < to.length; i++)
61             {
62                 sendTo[i] = new InternetAddress JavaDoc(to[i]);
63                 if (mLogger.isDebugEnabled()) mLogger.debug("sending e-mail to: " + to[i]);
64             }
65             message.setRecipients(Message.RecipientType.TO, sendTo);
66         }
67
68         if (cc != null)
69         {
70             InternetAddress JavaDoc[] copyTo = new InternetAddress JavaDoc[cc.length];
71
72             for (int i = 0; i < cc.length; i++)
73             {
74                 copyTo[i] = new InternetAddress JavaDoc(cc[i]);
75                 if (mLogger.isDebugEnabled()) mLogger.debug("copying e-mail to: " + cc[i]);
76             }
77             message.setRecipients(Message.RecipientType.CC, copyTo);
78         }
79
80         if (bcc != null)
81         {
82             InternetAddress JavaDoc[] copyTo = new InternetAddress JavaDoc[bcc.length];
83
84             for (int i = 0; i < bcc.length; i++)
85             {
86                 copyTo[i] = new InternetAddress JavaDoc(bcc[i]);
87                 if (mLogger.isDebugEnabled()) mLogger.debug("blind copying e-mail to: " + bcc[i]);
88             }
89             message.setRecipients(Message.RecipientType.BCC, copyTo);
90         }
91         message.setSubject((subject == null) ? "(no subject)" : subject);
92         message.setContent(content, mimeType);
93
94         // First collect all the addresses together.
95
Address JavaDoc[] remainingAddresses = message.getAllRecipients();
96         int nAddresses = remainingAddresses.length;
97         boolean bFailedToSome = false;
98         
99         SendFailedException JavaDoc sendex = new SendFailedException JavaDoc("Unable to send message to some recipients");
100         
101         // Try to send while there remain some potentially good addresses
102
do
103         {
104             // Avoid a loop if we are stuck
105
nAddresses = remainingAddresses.length;
106
107             try
108             {
109                 // Send to the list of remaining addresses, ignoring the addresses attached to the message
110
Transport.send(message,remainingAddresses);
111             }
112             catch(SendFailedException JavaDoc ex)
113             {
114                 bFailedToSome=true;
115                 sendex.setNextException(ex);
116                 
117                 // Extract the remaining potentially good addresses
118
remainingAddresses=ex.getValidUnsentAddresses();
119             }
120         } while (remainingAddresses!=null && remainingAddresses.length>0 && remainingAddresses.length!=nAddresses);
121         
122         if (bFailedToSome) throw sendex;
123     }
124
125     /**
126      * This method is used to send a Text Message.
127      *
128      * @param from e-mail address of sender
129      * @param to e-mail addresses of recipients
130      * @param subject subject of e-mail
131      * @param content the body of the e-mail
132      * @throws MessagingException the exception to indicate failure
133      */

134     public static void sendTextMessage
135     (
136         Session JavaDoc session,
137         String JavaDoc from,
138         String JavaDoc[] to,
139         String JavaDoc[] cc,
140         String JavaDoc[] bcc,
141         String JavaDoc subject,
142         String JavaDoc content
143     )
144     throws MessagingException JavaDoc
145     {
146         sendMessage(session, from, to, cc, bcc, subject, content, "text/plain; charset=utf-8");
147     }
148     
149     /**
150      * This method overrides the sendTextMessage to specify
151      * one receiver and mulitple cc recipients.
152      *
153      * @param from e-mail address of sender
154      * @param to e-mail addresses of recipients
155      * @param subject subject of e-mail
156      * @param content the body of the e-mail
157      * @throws MessagingException the exception to indicate failure
158      */

159     public static void sendTextMessage
160     (
161         Session JavaDoc session,
162         String JavaDoc from,
163         String JavaDoc to,
164         String JavaDoc[] cc,
165         String JavaDoc[] bcc,
166         String JavaDoc subject,
167         String JavaDoc content
168     )
169     throws MessagingException JavaDoc
170     {
171         String JavaDoc[] recipient = null;
172         if (to!=null) recipient = new String JavaDoc[] {to};
173
174         sendMessage(session, from, recipient, cc, bcc, subject, content, "text/plain; charset=utf-8");
175     }
176     
177     /**
178      * This method overrides the sendTextMessage to specify
179      * only one receiver and cc recipients, rather than
180      * an array of recipients.
181      *
182      * @param from e-mail address of sender
183      * @param to e-mail address of recipient
184      * @param cc e-mail address of cc recipient
185      * @param subject subject of e-mail
186      * @param content the body of the e-mail
187      * @throws MessagingException the exception to indicate failure
188      */

189     public static void sendTextMessage
190     (
191         Session JavaDoc session,
192         String JavaDoc from,
193         String JavaDoc to,
194         String JavaDoc cc,
195         String JavaDoc bcc,
196         String JavaDoc subject,
197         String JavaDoc content
198     )
199     throws MessagingException JavaDoc
200     {
201         String JavaDoc[] recipient = null;
202         String JavaDoc[] copy = null;
203         String JavaDoc[] bcopy = null;
204
205         if (to!=null) recipient = new String JavaDoc[] {to};
206         if (cc!=null) copy = new String JavaDoc[] {cc};
207         if (bcc!=null) bcopy = new String JavaDoc[] {bcc};
208
209         sendMessage(session, from, recipient, copy, bcopy, subject, content, "text/plain; charset=utf-8");
210     }
211     
212     /**
213      * This method is used to send a HTML Message
214      *
215      * @param from e-mail address of sender
216      * @param to e-mail address(es) of recipients
217      * @param subject subject of e-mail
218      * @param content the body of the e-mail
219      * @throws MessagingException the exception to indicate failure
220      */

221     public static void sendHTMLMessage
222     (
223         Session JavaDoc session,
224         String JavaDoc from,
225         String JavaDoc[] to,
226         String JavaDoc[] cc,
227         String JavaDoc[] bcc,
228         String JavaDoc subject,
229         String JavaDoc content
230     )
231     throws MessagingException JavaDoc
232     {
233         sendMessage(session, from, to, cc, bcc, subject, content, "text/html; charset=utf-8");
234     }
235     
236     /**
237      * This method overrides the sendHTMLMessage to specify
238      * only one sender, rather than an array of senders.
239      *
240      * @param from e-mail address of sender
241      * @param to e-mail address of recipients
242      * @param subject subject of e-mail
243      * @param content the body of the e-mail
244      * @throws MessagingException the exception to indicate failure
245      */

246     public static void sendHTMLMessage
247     (
248         Session JavaDoc session,
249         String JavaDoc from,
250         String JavaDoc to,
251         String JavaDoc cc,
252         String JavaDoc bcc,
253         String JavaDoc subject,
254         String JavaDoc content
255     )
256     throws MessagingException JavaDoc
257     {
258         String JavaDoc[] recipient = null;
259         String JavaDoc[] copy = null;
260         String JavaDoc[] bcopy = null;
261
262         if (to!=null) recipient = new String JavaDoc[] {to};
263         if (cc!=null) copy = new String JavaDoc[] {cc};
264         if (bcc!=null) bcopy = new String JavaDoc[] {bcc};
265
266         sendMessage(session, from, recipient, copy, bcopy, subject, content, "text/html; charset=utf-8");
267     }
268     
269     /**
270      * This method overrides the sendHTMLMessage to specify
271      * one receiver and mulitple cc recipients.
272      *
273      * @param from e-mail address of sender
274      * @param to e-mail address of recipient
275      * @param cc e-mail addresses of recipients
276      * @param subject subject of e-mail
277      * @param content the body of the e-mail
278      * @throws MessagingException the exception to indicate failure
279      */

280     public static void sendHTMLMessage
281     (
282         Session JavaDoc session,
283         String JavaDoc from,
284         String JavaDoc to,
285         String JavaDoc[] cc,
286         String JavaDoc[] bcc,
287         String JavaDoc subject,
288         String JavaDoc content
289     )
290     throws MessagingException JavaDoc
291     {
292         String JavaDoc[] recipient = null;
293         if (to!=null) recipient = new String JavaDoc[] {to};
294
295         sendMessage(session, from, recipient, cc, bcc, subject, content, "text/html; charset=utf-8");
296     }
297 }
298
299
Popular Tags