1 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 42 43 public class EmailServlet extends HttpServlet { 44 45 static String contextPath; 46 47 static String configLocation; 48 49 static String defaultMailHost; 50 51 String errorMsg; 52 53 61 public final void init(ServletConfig config) throws ServletException { 62 63 super.init(config); 64 65 ServletContext context = getServletContext(); 67 contextPath = context.getRealPath("/"); 68 69 defaultMailHost = config.getInitParameter("mailHost"); 70 71 } 72 73 85 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 86 87 doPost(req, res); 88 89 } 90 91 103 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 104 105 PrintWriter out = res.getWriter(); 106 HashMap glossary = new HashMap(); 107 String 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 e) { 127 e.printStackTrace(); 128 this.errorMsg = this.errorMsg + e.toString(); 129 sendErrorMessage(req, out, glossary); 130 } 131 132 } 133 134 136 146 private void sendErrorMessage(HttpServletRequest req, PrintWriter out, HashMap glossary) { 147 148 StringBuffer page = new StringBuffer (); 149 String COFAX_VERSION = (String ) 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("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 178 private boolean getParameters(HttpServletRequest req, HashMap glossary) { 179 180 String to = req.getParameter("email"); 181 String from = req.getParameter("from_email"); 182 String subject = req.getParameter("subject"); 183 184 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 redirectPath = req.getParameter("path"); 203 glossary.put("redirectPath", redirectPath + ""); 204 String msgBody = req.getParameter("message") + ""; 205 206 if (msgBody != null && redirectPath != null && msgBody.indexOf(redirectPath) > -1) { 208 String 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 224 private boolean sendMail(HashMap glossary, String hostName) { 225 226 try { 227 String from = CofaxUtil.getString(glossary, "msgFrom"); 228 String to = CofaxUtil.getString(glossary, "msgTo"); 229 String subject = CofaxUtil.getString(glossary, "msgSubject"); 230 StringBuffer text = new StringBuffer (); 231 Session smtpSession; 232 Properties props = new Properties(); 233 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 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 Transport.send(message); 256 } catch (Exception 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 284 289 public boolean smtpConnect() { 290 291 try { 292 } catch (Exception e) { 293 this.errorMsg = "Could not connect to system defined SMTP server!"; 294 return false; 295 } 296 return true; 297 } 298 299 304 public boolean smtpDisConnect() { 305 306 try { 307 308 } catch (Exception e) { 309 e.printStackTrace(); 310 return false; 311 } 312 return true; 313 } 314 315 } 316 | Popular Tags |