KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > MailHandler


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.util;
14
15 import java.util.Properties JavaDoc;
16
17 import javax.mail.Message JavaDoc;
18 import javax.mail.MessagingException JavaDoc;
19 import javax.mail.Session JavaDoc;
20 import javax.mail.Transport JavaDoc;
21 import javax.mail.internet.AddressException JavaDoc;
22 import javax.mail.internet.InternetAddress JavaDoc;
23 import javax.mail.internet.MimeMessage JavaDoc;
24
25 import org.apache.log4j.Logger;
26
27
28 /**
29  * This is a simple util class to send email from the form pages ... This class was previously included inside the jsp
30  * related to the form.
31  * @author niko
32  * @version $Id $
33  */

34 public class MailHandler {
35
36     /**
37      * Logger
38      */

39     private static Logger log = Logger.getLogger(MailHandler.class);
40
41     private String JavaDoc body;
42
43     private String JavaDoc subject;
44
45     private String JavaDoc from;
46
47     private InternetAddress JavaDoc[] toList;
48
49     private InternetAddress JavaDoc[] ccList;
50
51     private Session JavaDoc session;
52
53     public MailHandler(String JavaDoc mailHost, int toListLength, int ccListLength) throws Exception JavaDoc {
54         toList = new InternetAddress JavaDoc[toListLength];
55         ccList = new InternetAddress JavaDoc[ccListLength];
56         Properties JavaDoc props = System.getProperties();
57         props.put("mail.smtp.host", mailHost);
58         session = Session.getInstance(props, null);
59     }
60
61     public void sendMail() throws MessagingException JavaDoc {
62         try {
63             MimeMessage JavaDoc message = new MimeMessage JavaDoc(session);
64             message.setFrom(new InternetAddress JavaDoc(this.getFrom()));
65             message.setRecipients(Message.RecipientType.TO, this.getToList());
66             message.setRecipients(Message.RecipientType.CC, this.getCcList());
67             message.setSubject(subject);
68
69             message.setContent(body, "text/plain");
70             message.setHeader("Content-Type", "text/plain; charset=UTF-8");
71
72             Transport.send(message);
73             log.info("Mail has been sent to: [" + addressesToString(this.getToList()) + "]");
74         }
75         catch (MessagingException JavaDoc e) {
76             log.error("Email to: ["
77                 + addressesToString(this.getToList())
78                 + "] was not sent because of the following error: "
79                 + e.getMessage(), e);
80             throw e;
81         }
82         catch (RuntimeException JavaDoc e) {
83             // this is here in order to catch UnsupportedOperationException
84
// by alternative javamail libraries
85
log.error("Email to: ["
86                 + addressesToString(this.getToList())
87                 + "] was not sent because of the following error: "
88                 + e.getMessage(), e);
89             throw e;
90         }
91     }
92
93     /**
94      * Convert list of addresses to a nice string
95      * @param addresses list of internet addresses
96      * @return a comma-separated list of email addresses as a <code>String</code>
97      */

98     public String JavaDoc addressesToString(InternetAddress JavaDoc[] addresses) {
99         if (addresses == null)
100             return "";
101         int length = addresses.length;
102         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
103         for (int i = 0; i < length; i++) {
104             if (i != 0)
105                 sb.append(",");
106             sb.append(addresses[i].getAddress());
107         }
108         return sb.toString();
109     }
110
111     public void setBody(String JavaDoc body) {
112         this.body = body;
113     }
114
115     public String JavaDoc getBody() {
116         return this.body;
117     }
118
119     public void setSubject(String JavaDoc subject) {
120         this.subject = subject;
121     }
122
123     public String JavaDoc getSubject() {
124         return this.subject;
125     }
126
127     public void setFrom(String JavaDoc from) {
128         this.from = from;
129     }
130
131     public String JavaDoc getFrom() {
132         return this.from;
133     }
134
135     public void setToList(String JavaDoc to) throws AddressException JavaDoc {
136         String JavaDoc[] toObj = to.split("\n");
137         for (int i = 0; i < toObj.length; i++) {
138             this.toList[i] = new InternetAddress JavaDoc(toObj[i]);
139         }
140     }
141
142     public InternetAddress JavaDoc[] getToList() {
143         return this.toList;
144     }
145
146     public void setCcList(String JavaDoc to) throws AddressException JavaDoc {
147         String JavaDoc[] toObj = to.split("\n");
148         for (int i = 0; i < toObj.length; i++) {
149             this.ccList[i] = new InternetAddress JavaDoc(toObj[i]);
150         }
151     }
152
153     public InternetAddress JavaDoc[] getCcList() {
154         return this.ccList;
155
156     }
157 }
158
Popular Tags