/* * @(#)SendMailSurvey.java * * Copyright (c) 1998 Karl Moss. All Rights Reserved. * * You may study, use, modify, and distribute this software for any * purpose provided that this copyright notice appears in all copies. * * This software is provided WITHOUT WARRANTY either expressed or * implied. * * @author Karl Moss * @version 1.0 * @date 06Feb99 * */ package javaservlets.mail; import javax.servlet.*; import javax.servlet.http.*; import sun.net.smtp.*; /** *

This servlet will format an email form in HTML and, when * the user submits the form, will mail the message using * SMTP */ public class SendMailServlet extends HttpServlet { public static String MAIL_FROM = "from"; public static String MAIL_SUBJECT = "subject"; public static String MAIL_BODY = "body"; // Multiple 'to' addresses can be separated by commas public static String MAIL_TO = "karl@servletguru.com"; public static String MAIL_HOST = "server1.electronaut.com"; /** *

Performs the HTTP GET operation * * @param req The request from the client * @param resp The response from the servlet */ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { // Set the content type of the response resp.setContentType("text/html"); // Get the PrintWriter to write the response java.io.PrintWriter out = resp.getWriter(); // Create the HTML form out.println(""); out.println(""); out.println("Send Email"); out.println("

Send Email to Karl Moss

"); out.println("
"); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println("
From:
Subject:
Text:

"); out.println(""); out.println(""); out.println("
"); // Wrap up out.println(""); out.println(""); out.flush(); } /** *

Performs the HTTP POST operation * * @param req The request from the client * @param resp The response from the servlet */ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { // Set the content type of the response resp.setContentType("text/html"); // Create a PrintWriter to write the response java.io.PrintWriter out = new java.io.PrintWriter(resp.getOutputStream()); // Get the data from the form String from = req.getParameter(MAIL_FROM); String subject = req.getParameter(MAIL_SUBJECT); String body = req.getParameter(MAIL_BODY); try { // Create a new SMTP client SmtpClient mailer = new SmtpClient(MAIL_HOST); // Set the 'from' and 'to' addresses mailer.from(from); mailer.to(MAIL_TO); // Get the PrintStream for writing the rest of the message java.io.PrintStream ps = mailer.startMessage(); // Write out any mail headers ps.println("From: " + from); ps.println("To: " + MAIL_TO); ps.println("Subject: " + subject); // Write out the message body ps.println(body); // Send the message mailer.closeServer(); // Let the user know that the mail was sent out.println(""); out.println(""); out.println("Send Email"); out.println("

"); out.println("

Your email has been sent!

"); out.println("
"); } catch (Exception ex) { // Got an error sending the mail; notify the client out.println(""); out.println(""); out.println("Send Email Error"); out.println("
"); out.println("

There was an error sending your email

"); out.println("
Message=" + ex.getMessage()); out.println("
"); out.println(""); } // Wrap up out.flush(); } /** *

Initialize the servlet. This is called once when the * servlet is loaded. It is guaranteed to complete before any * requests are made to the servlet * * @param cfg Servlet configuration information */ public void init(ServletConfig cfg) throws ServletException { super.init(cfg); } /** *

Destroy the servlet. This is called once when the servlet * is unloaded. */ public void destroy() { super.destroy(); } }