KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > mail > MailService


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.util.mail;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.activation.DataHandler JavaDoc;
30 import javax.mail.Address JavaDoc;
31 import javax.mail.Message JavaDoc;
32 import javax.mail.MessagingException JavaDoc;
33 import javax.mail.Session JavaDoc;
34 import javax.mail.Transport JavaDoc;
35 import javax.mail.internet.AddressException JavaDoc;
36 import javax.mail.internet.InternetAddress JavaDoc;
37 import javax.mail.internet.MimeMessage JavaDoc;
38
39 import org.apache.commons.mail.HtmlEmail;
40 import org.apache.commons.mail.SimpleEmail;
41 import org.infoglue.cms.exception.Bug;
42 import org.infoglue.cms.exception.SystemException;
43 import org.infoglue.cms.util.CmsPropertyHandler;
44
45 public class MailService
46 {
47
48     // The mail session.
49
private Session JavaDoc session;
50
51
52     /**
53      * Creates a MailServices object and initializes it with the specified mail session.
54      */

55
56     public MailService(Session JavaDoc session)
57     {
58         this.session = session;
59     }
60
61     /**
62      *
63      */

64     public MimeMessage JavaDoc createMessage()
65     {
66         return new MimeMessage JavaDoc(this.session);
67     }
68     
69     /**
70      *
71      */

72     public void send(final Message JavaDoc message) throws SystemException
73     {
74         try
75         {
76             Transport.send(message);
77         }
78         catch(MessagingException JavaDoc e)
79         {
80             e.printStackTrace();
81             throw new SystemException("Unable to send message.", e);
82         }
83         
84     }
85
86     /**
87      *
88      * @param from the sender of the email.
89      * @param to the recipient of the email.
90      * @param subject the subject of the email.
91      * @param content the body of the email.
92      * @throws SystemException if the email couldn't be sent due to some mail server exception.
93      */

94     public void send(String JavaDoc from, String JavaDoc to, String JavaDoc bcc, String JavaDoc subject, String JavaDoc content) throws SystemException
95     {
96         send(createMessage(from, to, bcc, subject, content));
97     }
98
99     /**
100      *
101      * @param from the sender of the email.
102      * @param to the recipient of the email.
103      * @param subject the subject of the email.
104      * @param content the body of the email.
105      * @throws SystemException if the email couldn't be sent due to some mail server exception.
106      */

107     public void send(String JavaDoc from, String JavaDoc to, String JavaDoc subject, String JavaDoc content, String JavaDoc contentType, String JavaDoc encoding) throws SystemException
108     {
109         final Message JavaDoc message = createMessage(from, to, null, subject, content, contentType, encoding);
110      
111         try
112         {
113             Transport.send(message);
114         }
115         catch(MessagingException JavaDoc e)
116         {
117             e.printStackTrace();
118             throw new SystemException("Unable to send message.", e);
119         }
120
121     }
122
123     
124     /**
125      *
126      * @param from the sender of the email.
127      * @param to the recipient of the email.
128      * @param subject the subject of the email.
129      * @param content the body of the email.
130      * @throws SystemException if the email couldn't be sent due to some mail server exception.
131      */

132     public void sendEmail(String JavaDoc from, String JavaDoc to, String JavaDoc bcc, String JavaDoc subject, String JavaDoc content, String JavaDoc encoding) throws SystemException
133     {
134         String JavaDoc contentType = CmsPropertyHandler.getMailContentType();
135         if(contentType == null || contentType.length() == 0)
136             contentType = "text/html";
137
138         if(contentType.equalsIgnoreCase("text/html"))
139             sendHTML(from, to, bcc, subject, content, encoding);
140         else
141             sendPlain(from, to, bcc, subject, content, encoding);
142     }
143     
144     /**
145      *
146      * @param from the sender of the email.
147      * @param to the recipient of the email.
148      * @param subject the subject of the email.
149      * @param content the body of the email.
150      * @throws SystemException if the email couldn't be sent due to some mail server exception.
151      */

152     public void sendHTML(String JavaDoc from, String JavaDoc to, String JavaDoc bcc, String JavaDoc subject, String JavaDoc content, String JavaDoc encoding) throws SystemException
153     {
154         try
155         {
156             HtmlEmail email = new HtmlEmail();
157             String JavaDoc mailServer = CmsPropertyHandler.getMailSmtpHost();
158             String JavaDoc systemEmailSender = CmsPropertyHandler.getSystemEmailSender();
159             
160             email.setHostName(mailServer);
161
162             boolean needsAuthentication = false;
163             try
164             {
165                 needsAuthentication = new Boolean JavaDoc(CmsPropertyHandler.getMailSmtpAuth()).booleanValue();
166             }
167             catch (Exception JavaDoc ex)
168             {
169                 needsAuthentication = false;
170             }
171             
172             if (needsAuthentication)
173             {
174                 final String JavaDoc userName = CmsPropertyHandler.getMailSmtpUser();
175                 final String JavaDoc password = CmsPropertyHandler.getMailSmtpPassword();
176                 
177                 email.setAuthentication(userName, password);
178             }
179             
180             email.setBounceAddress(systemEmailSender);
181             email.setCharset(encoding);
182            
183             email.addTo(to, to);
184             email.setFrom(from, from);
185             if(bcc != null)
186                 email.setBcc(createInternetAddressesList(bcc));
187             email.setSubject(subject);
188             
189             email.setHtmlMsg(content);
190     
191             email.setTextMsg("Your email client does not support HTML messages");
192     
193             email.send();
194         }
195         catch (Exception JavaDoc e)
196         {
197             e.printStackTrace();
198             throw new SystemException("An error occurred when we tried to send this mail:" + e.getMessage(), e);
199         }
200         
201         /*
202         final Message message = createMessage(from, to, bcc, subject, content, contentType, encoding);
203      
204         try
205         {
206             Transport.send(message);
207         }
208         catch(MessagingException e)
209         {
210             e.printStackTrace();
211             throw new SystemException("Unable to send message.", e);
212         }
213         */

214     }
215
216
217     /**
218      *
219      * @param from the sender of the email.
220      * @param to the recipient of the email.
221      * @param subject the subject of the email.
222      * @param content the body of the email.
223      * @throws SystemException if the email couldn't be sent due to some mail server exception.
224      */

225     public void sendPlain(String JavaDoc from, String JavaDoc to, String JavaDoc bcc, String JavaDoc subject, String JavaDoc content, String JavaDoc encoding) throws SystemException
226     {
227         try
228         {
229             SimpleEmail email = new SimpleEmail();
230             String JavaDoc mailServer = CmsPropertyHandler.getMailSmtpHost();
231             String JavaDoc systemEmailSender = CmsPropertyHandler.getSystemEmailSender();
232             
233             email.setHostName(mailServer);
234
235             boolean needsAuthentication = false;
236             try
237             {
238                 needsAuthentication = new Boolean JavaDoc(CmsPropertyHandler.getMailSmtpAuth()).booleanValue();
239             }
240             catch (Exception JavaDoc ex)
241             {
242                 needsAuthentication = false;
243             }
244             
245             if (needsAuthentication)
246             {
247                 final String JavaDoc userName = CmsPropertyHandler.getMailSmtpUser();
248                 final String JavaDoc password = CmsPropertyHandler.getMailSmtpPassword();
249                 
250                 email.setAuthentication(userName, password);
251             }
252             
253             email.setBounceAddress(systemEmailSender);
254             email.setCharset(encoding);
255            
256             email.addTo(to, to);
257             email.setFrom(from, from);
258             email.setBcc(createInternetAddressesList(bcc));
259             email.setSubject(subject);
260             email.setMsg(content);
261     
262             email.send();
263         }
264         catch (Exception JavaDoc e)
265         {
266             e.printStackTrace();
267         }
268         
269         /*
270         final Message message = createMessage(from, to, bcc, subject, content, contentType, encoding);
271      
272         try
273         {
274             Transport.send(message);
275         }
276         catch(MessagingException e)
277         {
278             e.printStackTrace();
279             throw new SystemException("Unable to send message.", e);
280         }
281         */

282     }
283
284     /**
285      *
286      */

287     private Message JavaDoc createMessage(String JavaDoc from, String JavaDoc to, String JavaDoc bcc, String JavaDoc subject, String JavaDoc content) throws SystemException
288     {
289         try
290         {
291             final Message JavaDoc message = new MimeMessage JavaDoc(this.session);
292
293             message.setContent(content, "text/html");
294             message.setFrom(createInternetAddress(from));
295             message.setRecipient(Message.RecipientType.TO, createInternetAddress(to));
296             if(bcc != null)
297                 message.setRecipients(Message.RecipientType.BCC, createInternetAddresses(bcc));
298             message.setSubject(subject);
299             message.setText(content);
300             message.setDataHandler(new DataHandler JavaDoc(new StringDataSource(content, "text/html")));
301     
302             return message;
303         }
304         catch(MessagingException JavaDoc e)
305         {
306             throw new Bug("Unable to create the message.", e);
307         }
308     }
309
310     /**
311      *
312      */

313     private Message JavaDoc createMessage(String JavaDoc from, String JavaDoc to, String JavaDoc bcc, String JavaDoc subject, String JavaDoc content, String JavaDoc contentType, String JavaDoc encoding) throws SystemException
314     {
315         try
316         {
317             final Message JavaDoc message = new MimeMessage JavaDoc(this.session);
318             String JavaDoc contentTypeWithEncoding = contentType+";charset="+encoding;
319
320             //message.setContent(content, contentType);
321
message.setFrom(createInternetAddress(from));
322             message.setRecipient(Message.RecipientType.TO, createInternetAddress(to));
323             if(bcc != null)
324                 message.setRecipients(Message.RecipientType.BCC, createInternetAddresses(bcc));
325             //message.setSubject(subject);
326

327             ((MimeMessage JavaDoc)message).setSubject(subject, encoding);
328             //message.setText(content);
329
message.setDataHandler(new DataHandler JavaDoc(new StringDataSource(content, contentTypeWithEncoding, encoding)));
330             //message.setText(content);
331
//message.setDataHandler(new DataHandler(new StringDataSource(content, "text/html")));
332

333             return message;
334         }
335         catch(MessagingException JavaDoc e)
336         {
337             throw new Bug("Unable to create the message.", e);
338         }
339     }
340     
341     /**
342      *
343      */

344     private Address JavaDoc createInternetAddress(String JavaDoc address)
345     {
346         try
347         {
348             return new InternetAddress JavaDoc(address);
349         }
350         catch(AddressException JavaDoc e)
351         {
352             throw new Bug("Badly formatted email address [" + address + "].", e);
353         }
354     }
355     
356     /**
357      *
358      */

359     private Address JavaDoc[] createInternetAddresses(String JavaDoc emailAddressString) throws SystemException
360     {
361         String JavaDoc[] emailAddresses = emailAddressString.split(";");
362         
363         Address JavaDoc[] addresses = new Address JavaDoc[emailAddresses.length];
364         for(int i=0; i<emailAddresses.length; i++)
365         {
366             String JavaDoc email = emailAddresses[i];
367             try
368             {
369                 addresses[i] = new InternetAddress JavaDoc(email);
370             }
371             catch(AddressException JavaDoc e)
372             {
373                 throw new SystemException("Badly formatted email address [" + email + "].", e);
374             }
375         }
376         
377         return addresses;
378     }
379
380     /**
381      *
382      */

383     private List JavaDoc createInternetAddressesList(String JavaDoc emailAddressString) throws SystemException
384     {
385         String JavaDoc[] emailAddresses = emailAddressString.split(";");
386         
387         List JavaDoc addresses = new ArrayList JavaDoc();
388         for(int i=0; i<emailAddresses.length; i++)
389         {
390             String JavaDoc email = emailAddresses[i];
391             try
392             {
393                 addresses.add(new InternetAddress JavaDoc(email));
394             }
395             catch(AddressException JavaDoc e)
396             {
397                 throw new SystemException("Badly formatted email address [" + email + "].", e);
398             }
399         }
400         
401         return addresses;
402     }
403     
404 }
Popular Tags