Developing
the E-Mail Sender Servlet
Gopalan Suresh Raj
Note |
To
work with any of these samples, you will need the
following: .........................................JDK 1.1.7 or higher To work with the Servlets and JSP, you will need .........................................JSWDK 1.0.1 or higher To work with the Java Mail API, you will need .........................................JavaMail 1.1.3 or higher .........................................Java Activation Framework 1.0.1 or higher .........................................POP3 Provider 1.1.1 or higher |
1. Develop the EmailSendServlet class
Develop the EmailSendServlet and compile the piece of code to generate the class file.
The JSP Model 2
Architecture
(Model-View-Controller Based)
Figure shows what we are
trying to ultimately accomplish in these pages here.
EmailSendServlet.java |
import java.util.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import EmailHandler.*; public class EmailSendServlet extends HttpServlet { private static String ERROR_URL = "/EmailSendError.html"; private static String REDIRECT_URL = "/jsp/EmailHandler/EmailContents.jsp"; // Substitute the following code with your SMTP mail Host name private static String SMTP_HOST = "mail.myProvider.com"; public void init (ServletConfig config) throws ServletException { super.init (config); } public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession (false); if (null == session) { response.sendRedirect (ERROR_URL); } String from = request.getParameter ("FromBox"); String toStream = request.getParameter ("ToBox"); String ccStream = request.getParameter ("CcBox"); String bccStream = request.getParameter ("BccBox"); String subject = request.getParameter ("SubjectBox"); String body = request.getParameter ("BodyBox"); Vector to = getValuesFromStream (toStream); Vector cc = getValuesFromStream (ccStream); Vector bcc = getValuesFromStream (bccStream); Vector emailList = new Vector (); EmailContents emailContents = new EmailContents (from, to, cc, bcc, subject, body); emailList.addElement (emailContents); session.putValue ("EmailHandler.emailContents", emailList); SendMultipartMessage message = new SendMultipartMessage (emailContents); boolean result = message.sendMail (SMTP_HOST); if(false == result) { response.sendRedirect (ERROR_URL); } else { ServletContext context = getServletContext (); RequestDispatcher dispatcher = context.getRequestDispatcher (REDIRECT_URL); dispatcher.forward (request, response); } } private Vector getValuesFromStream (String stream) { Vector holder = new Vector(); String element = null; int i = 0; StringTokenizer tokenizer = new StringTokenizer (stream, ",;"); while (tokenizer.hasMoreTokens()) { element = tokenizer.nextToken(); holder.addElement (element); System.out.println(element); } return holder; } }
|
2. Compile this code and Move the class file to the servlets directory at "jswdk-1.0.1\examples\WEB-INF\servlets\".
click here to go to
My JSP/Servlets HomePage...
About the Author... |
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He is contributing author to a couple of books "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia© site (http://www.execpc.com/~gopalan) or mail him at gopalan@execpc.com. |
This site was developed and is maintained by Gopalan Suresh Raj This page has been visited times since September 21,1998. |
Last Updated : Dec 19, '98 |
Copyright (c) 1997-2000, Gopalan Suresh Raj - All rights reserved. Terms of use. |
All products and companies mentioned at this site are trademarks of their respective owners. |