FileDocCategorySizeDatePackage
MailServlet.javaAPI DocExample1772Tue Jan 25 10:45:14 GMT 2000None

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 {
      SmtpClient smtp = new SmtpClient();  // assume localhost
      smtp.from(from);
      smtp.to(to);
      PrintStream msg = smtp.startMessage();

      msg.println("To: " + to);  // so mailers will display the To: address
      msg.println("Subject: Customer feedback");
      msg.println();

      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);
        msg.println(name + " = " + value);
      }

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

      smtp.closeServer();

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