KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > cds > EmailServlet


1 /*
2  * EmailServlet is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/cds/EmailServlet.java,v 1.4.2.1 2006/12/11 16:19:29 fxrobin Exp $
21  */

22
23 package org.cofax.cds;
24
25 import org.cofax.*;
26 import javax.servlet.*;
27 import javax.servlet.http.*;
28 import java.io.*;
29 import java.util.*;
30 import javax.mail.*;
31 import javax.mail.internet.*;
32
33 /**
34  * EmailServlet.java provides e-mail functionality for cofax "aemail" template.
35  * Last Updated 10/03/2001 -- This code was entirly redone using java.mail
36  * instead of sun.net.smtp.SmtpClient. -- Sam Cohen
37  *
38  * @author Sam Cohen
39  * @created May 7, 2002
40  * @version 1.9.7
41  */

42
43 public class EmailServlet extends HttpServlet {
44
45     static String JavaDoc contextPath;
46
47     static String JavaDoc configLocation;
48
49     static String JavaDoc defaultMailHost;
50
51     String JavaDoc errorMsg;
52
53     /**
54      * Description of the Method
55      *
56      * @param config
57      * Description of the Parameter
58      * @exception ServletException
59      * Description of the Exception
60      */

61     public final void init(ServletConfig config) throws ServletException {
62
63         super.init(config);
64
65         // Get the servlet's context information.
66
ServletContext context = getServletContext();
67         contextPath = context.getRealPath("/");
68
69         defaultMailHost = config.getInitParameter("mailHost");
70
71     }
72
73     /**
74      * Description of the Method
75      *
76      * @param req
77      * Description of the Parameter
78      * @param res
79      * Description of the Parameter
80      * @exception ServletException
81      * Description of the Exception
82      * @exception IOException
83      * Description of the Exception
84      */

85     public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
86
87         doPost(req, res);
88
89     }
90
91     /**
92      * Description of the Method
93      *
94      * @param req
95      * Description of the Parameter
96      * @param res
97      * Description of the Parameter
98      * @exception ServletException
99      * Description of the Exception
100      * @exception IOException
101      * Description of the Exception
102      */

103     public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
104
105         PrintWriter out = res.getWriter();
106         HashMap glossary = new HashMap();
107         String JavaDoc hostName = "";
108
109         try {
110             this.errorMsg = "";
111
112             hostName = defaultMailHost;
113
114             res.setContentType("text/html");
115             if (!getParameters(req, glossary)) {
116                 sendErrorMessage(req, out, glossary);
117                 return;
118             }
119
120             if (!sendMail(glossary, hostName)) {
121                 sendErrorMessage(req, out, glossary);
122                 return;
123             } else {
124                 res.sendRedirect(CofaxUtil.getString(glossary, "redirectPath"));
125             }
126         } catch (Exception JavaDoc e) {
127             e.printStackTrace();
128             this.errorMsg = this.errorMsg + e.toString();
129             sendErrorMessage(req, out, glossary);
130         }
131
132     }
133
134     // end doPost
135

136     /**
137      * Description of the Method
138      *
139      * @param req
140      * Description of the Parameter
141      * @param out
142      * Description of the Parameter
143      * @param glossary
144      * Description of the Parameter
145      */

146     private void sendErrorMessage(HttpServletRequest req, PrintWriter out, HashMap glossary) {
147
148         StringBuffer JavaDoc page = new StringBuffer JavaDoc();
149         String JavaDoc COFAX_VERSION = (String JavaDoc) getServletContext().getAttribute("COFAX_VERSION");
150
151         page.append("<title>Cofax Error -- " + req.getRequestURI() + "</title>\n");
152         page.append("<body bgcolor=FFFFFF>\n");
153         page.append("<img SRC=http://www.cofax.org/images/cofax.gif>\n");
154         page.append(COFAX_VERSION + "<br>\n");
155         page.append("Cofax has encountered and error while processing your request.<br><br>\n");
156         page.append("<dd><font color=FF2121>Error Message:</font> " + this.errorMsg + "<br>\n");
157         page.append("<dd>Request: " + req.getRequestURI() + "<br>\n");
158         // page.append("<dd>Template: " + templateId + "<br><br>\n") ;
159
page.append("Please try one of the following:<ul>\n");
160         page.append("<li><a HREF=javascript:window.location.reload()>reload this page</a></li>\n");
161         page.append("<li><a HREF=javascript:window.history.back()>return to your previous page</a></li>\n");
162         page.append("<li><a HREF=" + req.getContextPath() + "/" + CofaxUtil.getString(glossary, "request:pubName") + ">");
163         page.append("go to the home for this publication</a></li>\n");
164         page.append("<li><a HREF=mailto:info@cofax.org>contact Cofax</a></li></ul>\n");
165         out.println(page.toString());
166
167     }
168
169     /**
170      * Gets the parameters attribute of the EmailServlet object
171      *
172      * @param req
173      * Description of the Parameter
174      * @param glossary
175      * Description of the Parameter
176      * @return The parameters value
177      */

178     private boolean getParameters(HttpServletRequest req, HashMap glossary) {
179
180         String JavaDoc to = req.getParameter("email");
181         String JavaDoc from = req.getParameter("from_email");
182         String JavaDoc subject = req.getParameter("subject");
183
184         // check for valid email addresses
185
if (from == null || to == null || from.equals("") || to.equals("")) {
186             this.errorMsg = "To and From can not be blank";
187             return false;
188         }
189         if (from.indexOf("@") == -1 || to.indexOf("@") == -1) {
190             this.errorMsg = "Both To and From must be valid email addresses!(@)";
191             return false;
192         }
193         if (from.indexOf(".") == -1 || to.indexOf(".") == -1) {
194             this.errorMsg = "Both To and From must be valid email addresses!(.)";
195             return false;
196         }
197
198         glossary.put("msgSubject", subject);
199         glossary.put("msgFrom", from);
200         glossary.put("msgTo", to);
201         glossary.put("msgPromotion", req.getParameter("promoMSG"));
202         String JavaDoc redirectPath = req.getParameter("path");
203         glossary.put("redirectPath", redirectPath + "");
204         String JavaDoc msgBody = req.getParameter("message") + "";
205
206         // adjust msgBody with a path if necessary
207
if (msgBody != null && redirectPath != null && msgBody.indexOf(redirectPath) > -1) {
208             String JavaDoc linkPath = "<a HREF=\"" + redirectPath + "\">" + redirectPath + "</a>";
209             msgBody = CofaxUtil.replace(msgBody, redirectPath, linkPath);
210         }
211         glossary.put("msgBody", msgBody);
212         return true;
213     }
214
215     /**
216      * Description of the Method
217      *
218      * @param glossary
219      * Description of the Parameter
220      * @param hostName
221      * Description of the Parameter
222      * @return Description of the Return Value
223      */

224     private boolean sendMail(HashMap glossary, String JavaDoc hostName) {
225
226         try {
227             String JavaDoc from = CofaxUtil.getString(glossary, "msgFrom");
228             String JavaDoc to = CofaxUtil.getString(glossary, "msgTo");
229             String JavaDoc subject = CofaxUtil.getString(glossary, "msgSubject");
230             StringBuffer JavaDoc text = new StringBuffer JavaDoc();
231             Session smtpSession;
232             Properties props = new Properties();
233             // System.getProperties() ;
234

235             props.put("mail.smtp.host", hostName);
236             smtpSession = Session.getDefaultInstance(props, null);
237
238             text.append("This Story has been sent to you by : " + from);
239             text.append("<pre>\n");
240             text.append(CofaxUtil.getString(glossary, "msgBody"));
241             text.append("\n");
242             text.append("</pre><br>\n");
243             text.append("<p>");
244             text.append(CofaxUtil.getString(glossary, "msgPromotion"));
245             text.append("\n");
246
247             // Define message
248
MimeMessage message = new MimeMessage(smtpSession);
249             message.setFrom(new InternetAddress(from));
250             message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
251             message.setSubject(subject);
252             message.setContent(text.toString(), "text/html");
253
254             // send message
255
Transport.send(message);
256         } catch (Exception JavaDoc e) {
257             this.errorMsg = "An error occured while trying to send your message!";
258             this.errorMsg = this.errorMsg + " Please be sure the to and from addresses are valid.";
259             return false;
260         }
261         return true;
262     }
263
264     /*
265      * PrintStream out ; SmtpClient send ; / Replace the following with your
266      * outgoing mail server name / send = new
267      * SmtpClient("mail.staff.philly.com") ; send = new SmtpClient(hostName) ;
268      * send.from(CofaxUtil.getString(glossary, "msgFrom")) ;
269      * send.to(CofaxUtil.getString(glossary, "msgTo")) ; out =
270      * send.startMessage() ; out.println("From: " +
271      * CofaxUtil.getString(glossary, "msgFrom")) ; out.println("To: " +
272      * CofaxUtil.getString(glossary, "msgTo")) ; out.println("Subject: " +
273      * CofaxUtil.getString(glossary, "msgSubject")) ; out.println("Content-type:
274      * text/html") ; out.println("\n") ; out.println("<br><pre>") ;
275      * out.println(CofaxUtil.getString(glossary, "msgBody")) ; out.println("\n") ;
276      * out.println("</pre><br>") ; out.println("This Story has been sent to
277      * you by : " + CofaxUtil.getString(glossary, "msgFrom")) ;
278      * out.println("\n") ; out.println("<p>") ;
279      * out.println(CofaxUtil.getString(glossary, "msgPromotion")) ;
280      * out.println("\n") ; out.flush() ; out.close() ; send.closeServer() ; }
281      * catch (IOException e) { log("Error occurred while sending mail", e) ;
282      * return false ; } return true ; } //end sendEmail method
283      */

284     /**
285      * Description of the Method
286      *
287      * @return Description of the Return Value
288      */

289     public boolean smtpConnect() {
290
291         try {
292         } catch (Exception JavaDoc e) {
293             this.errorMsg = "Could not connect to system defined SMTP server!";
294             return false;
295         }
296         return true;
297     }
298
299     /**
300      * Description of the Method
301      *
302      * @return Description of the Return Value
303      */

304     public boolean smtpDisConnect() {
305
306         try {
307
308         } catch (Exception JavaDoc e) {
309             e.printStackTrace();
310             return false;
311         }
312         return true;
313     }
314
315 }
316
Popular Tags