FileDocCategorySizeDatePackage
ContactServlet.javaAPI DocExample3106Sun Feb 10 09:38:58 GMT 2002None

ContactServlet

public class ContactServlet extends HttpServlet

Fields Summary
protected String
SMTPSERVER
The mail server, usually localhost. Should be a parameter.
protected String
MYADDR
Contact. Gotten from a parameter.
protected Session
session
The JavaMail session object XXX Is it threadsafe?
Constructors Summary
Methods Summary
public voidinit()


	     

		Properties props = new Properties();
		props.put("mail.smtp.host", SMTPSERVER);

		// Create the Mail Session object
		session = Session.getDefaultInstance(props, null);
		session.setDebug(true);		// Verbose!

		ServletContext ctx = getServletContext();
		MYADDR = ctx.getInitParameter("contact.email");
		if (MYADDR == null) {
			String error = "contact.email not set!!";
			log(error);
			throw new ServletException(error);
		}
	
public voidservice(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)


		ServletContext ctx = getServletContext();
		response.setContentType("text/html");

		PrintWriter out = response.getWriter();
	
		String hostIP = request.getRemoteAddr();

		String message_body = 
			"[This message is from host " +  hostIP + "]\n\n" +
			request.getParameter("message") +
			"\n";

		// out.println("<pre>" + message_body + "</pre>");

		try {

			/** Create a JavaMail message object */
			Message mesg = new MimeMessage(session);

			// TO (me) Address - should come from a Parameter...
			mesg.addRecipient(Message.RecipientType.TO,
				new InternetAddress(MYADDR));

			// FROM Address : request.name <request.email>
			mesg.setFrom(new InternetAddress(
				request.getParameter("name") + " <" +
				request.getParameter("email") + ">"));

			// The Subject
			String subject = request.getParameter("subject");
			if (subject == null || subject.length() == 0)
				subject = "DarwinSys Contact Form";
			mesg.setSubject(subject);

			// Now the message body.
			mesg.setText(message_body);
			// XXX I18N: use setText(msgText.getText(), charset)
			
			// Send the message!
			Transport.send(mesg);

			// No errors found at send time, confirm OK
			RequestDispatcher rd = 
				ctx.getRequestDispatcher("/contact-ack.jsp");
			rd.forward(request, response);

		} catch (MessagingException ex) {
			// Or something went wrong, complain to user.
			out.println("<html>");
			out.println("<head><title>Mail Error</title></head>");
			out.println("<body text=\"white\" bgcolor=\"black\">");
			out.println("<h1>Mail error</h1>");
			out.println("Rats! An error occurred sending a mail notification");
			out.println("<pre>");
			while ((ex = (MessagingException)ex.getNextException()) != null) {
				ex.printStackTrace(out);
			}
			out.println("</body>");
			out.println("</html>");
		} finally {
			out.flush();
			out.close();
		}