KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 package org.apache.roller.util;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import javax.mail.Message JavaDoc;
24 import javax.mail.MessagingException JavaDoc;
25 import javax.mail.SendFailedException JavaDoc;
26 import javax.mail.Session JavaDoc;
27 import javax.mail.Transport JavaDoc;
28 import javax.mail.Address JavaDoc;
29 import javax.mail.internet.InternetAddress JavaDoc;
30 import javax.mail.internet.MimeMessage JavaDoc;
31 import org.apache.commons.lang.StringUtils;
32
33 public class MailUtil extends Object JavaDoc {
34    
35     private static Log mLogger =
36         LogFactory.getFactory().getInstance(MailUtil.class);
37
38     // agangolli: Incorporated suggested changes from Ken Blackler.
39

40     /**
41      * This method is used to send a Message with a pre-defined
42      * mime-type.
43      *
44      * @param from e-mail address of sender
45      * @param to e-mail address(es) of recipients
46      * @param subject subject of e-mail
47      * @param content the body of the e-mail
48      * @param mimeType type of message, i.e. text/plain or text/html
49      * @throws MessagingException the exception to indicate failure
50      */

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

152     public static void sendTextMessage
153     (
154         Session JavaDoc session,
155         String JavaDoc from,
156         String JavaDoc[] to,
157         String JavaDoc[] cc,
158         String JavaDoc[] bcc,
159         String JavaDoc subject,
160         String JavaDoc content
161     )
162     throws MessagingException JavaDoc
163     {
164         sendMessage(session, from, to, cc, bcc, subject, content, "text/plain; charset=utf-8");
165     }
166     
167     /**
168      * This method overrides the sendTextMessage to specify
169      * one receiver and mulitple cc recipients.
170      *
171      * @param from e-mail address of sender
172      * @param to e-mail addresses of recipients
173      * @param subject subject of e-mail
174      * @param content the body of the e-mail
175      * @throws MessagingException the exception to indicate failure
176      */

177     public static void sendTextMessage
178     (
179         Session JavaDoc session,
180         String JavaDoc from,
181         String JavaDoc to,
182         String JavaDoc[] cc,
183         String JavaDoc[] bcc,
184         String JavaDoc subject,
185         String JavaDoc content
186     )
187     throws MessagingException JavaDoc
188     {
189         String JavaDoc[] recipient = null;
190         if (to!=null) recipient = new String JavaDoc[] {to};
191
192         sendMessage(session, from, recipient, cc, bcc, subject, content, "text/plain; charset=utf-8");
193     }
194     
195     /**
196      * This method overrides the sendTextMessage to specify
197      * only one receiver and cc recipients, rather than
198      * an array of recipients.
199      *
200      * @param from e-mail address of sender
201      * @param to e-mail address of recipient
202      * @param cc e-mail address of cc recipient
203      * @param subject subject of e-mail
204      * @param content the body of the e-mail
205      * @throws MessagingException the exception to indicate failure
206      */

207     public static void sendTextMessage
208     (
209         Session JavaDoc session,
210         String JavaDoc from,
211         String JavaDoc to,
212         String JavaDoc cc,
213         String JavaDoc bcc,
214         String JavaDoc subject,
215         String JavaDoc content
216     )
217     throws MessagingException JavaDoc
218     {
219         String JavaDoc[] recipient = null;
220         String JavaDoc[] copy = null;
221         String JavaDoc[] bcopy = null;
222
223         if (to!=null) recipient = new String JavaDoc[] {to};
224         if (cc!=null) copy = new String JavaDoc[] {cc};
225         if (bcc!=null) bcopy = new String JavaDoc[] {bcc};
226
227         sendMessage(session, from, recipient, copy, bcopy, subject, content, "text/plain; charset=utf-8");
228     }
229     
230     /**
231      * This method is used to send a HTML Message
232      *
233      * @param from e-mail address of sender
234      * @param to e-mail address(es) of recipients
235      * @param subject subject of e-mail
236      * @param content the body of the e-mail
237      * @throws MessagingException the exception to indicate failure
238      */

239     public static void sendHTMLMessage
240     (
241         Session JavaDoc session,
242         String JavaDoc from,
243         String JavaDoc[] to,
244         String JavaDoc[] cc,
245         String JavaDoc[] bcc,
246         String JavaDoc subject,
247         String JavaDoc content
248     )
249     throws MessagingException JavaDoc
250     {
251         sendMessage(session, from, to, cc, bcc, subject, content, "text/html; charset=utf-8");
252     }
253     
254     /**
255      * This method overrides the sendHTMLMessage to specify
256      * only one sender, rather than an array of senders.
257      *
258      * @param from e-mail address of sender
259      * @param to e-mail address of recipients
260      * @param subject subject of e-mail
261      * @param content the body of the e-mail
262      * @throws MessagingException the exception to indicate failure
263      */

264     public static void sendHTMLMessage
265     (
266         Session JavaDoc session,
267         String JavaDoc from,
268         String JavaDoc to,
269         String JavaDoc cc,
270         String JavaDoc bcc,
271         String JavaDoc subject,
272         String JavaDoc content
273     )
274     throws MessagingException JavaDoc
275     {
276         String JavaDoc[] recipient = null;
277         String JavaDoc[] copy = null;
278         String JavaDoc[] bcopy = null;
279
280         if (to!=null) recipient = new String JavaDoc[] {to};
281         if (cc!=null) copy = new String JavaDoc[] {cc};
282         if (bcc!=null) bcopy = new String JavaDoc[] {bcc};
283
284         sendMessage(session, from, recipient, copy, bcopy, subject, content, "text/html; charset=utf-8");
285     }
286     
287     /**
288      * This method overrides the sendHTMLMessage to specify
289      * one receiver and mulitple cc recipients.
290      *
291      * @param from e-mail address of sender
292      * @param to e-mail address of recipient
293      * @param cc e-mail addresses of recipients
294      * @param subject subject of e-mail
295      * @param content the body of the e-mail
296      * @throws MessagingException the exception to indicate failure
297      */

298     public static void sendHTMLMessage
299     (
300         Session JavaDoc session,
301         String JavaDoc from,
302         String JavaDoc to,
303         String JavaDoc[] cc,
304         String JavaDoc[] bcc,
305         String JavaDoc subject,
306         String JavaDoc content
307     )
308     throws MessagingException JavaDoc
309     {
310         String JavaDoc[] recipient = null;
311         if (to!=null) recipient = new String JavaDoc[] {to};
312
313         sendMessage(session, from, recipient, cc, bcc, subject, content, "text/html; charset=utf-8");
314     }
315 }
316
317
Popular Tags