FileDocCategorySizeDatePackage
MailServlet.javaAPI DocExample1713Thu Apr 05 20:38:42 BST 2001None

MailServlet

public class MailServlet extends HttpServlet

Fields Summary
static final String
FROM
static final String
TO
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)


       
                                  
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();

    ParameterParser parser = new ParameterParser(req);
    String from = parser.getStringParameter("from", FROM);
    String to = parser.getStringParameter("to", TO);

    try {
      MailMessage msg = new MailMessage();  // assume localhost
      msg.from(from);
      msg.to(to);
      msg.setSubject("Customer feedback");

      PrintStream body = msg.getPrintStream();

      Enumeration enum = req.getParameterNames();
      while (enum.hasMoreElements()) {
        String name = (String)enum.nextElement();
        if (name.equals("to") || name.equals("from")) continue; // Skip to/from
        String value = parser.getStringParameter(name, null);
        body.println(name + " = " + value);
      }

      body.println();
      body.println("---");
      body.println("Sent by " + HttpUtils.getRequestURL(req));

      msg.sendAndClose();

      out.println("Thanks for the submission...");
    }
    catch (IOException e) {
      out.println("There was a problem handling the submission...");
      log("There was a problem sending email", e);
    }