FileDocCategorySizeDatePackage
MailSendServlet.javaAPI DocExample1920Mon Dec 31 19:19:06 GMT 2001com.darwinsys.webmail

MailSendServlet

public class MailSendServlet extends HttpServlet
Servlet that sends email. BEWARE of security implications!

Fields Summary
Constructors Summary
Methods Summary
public voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Called to process each request.

		HttpSession session = request.getSession(false);
		if (session == null ||
			session.getAttribute("jabadot.user") == null) {
			out.println("<h1>Error</h1>");
			out.println("<p>You must be logged in before you can send mail!</p>");
			log("Non-logged-in mail servlet from " + request.getHost());
			return;
		}

      String title = "Mail Sender Servlet";
      String text = request.getParameter("text");
      String recipient = request.getParameter("recipient");
      String subject = request.getParameter("subject");
      
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      
      out.println("<html><head><title>");
      out.println(title);
      out.println("</title></head><body>");
      out.println("<h1>" + title + "</h1>");     
      out.println("<h2>Calling Mailer...</h2>");

      try
      {
            MimeMessage m = new MimeMessage(session);
            m.setFrom("ian@darwinsys.com");
            Address[] to = new InternetAddress[] {
                new InternetAddress(recipient)
            };
            m.setRecipients(Message.RecipientType.TO, to);
            m.setSubject(subject);
            m.setSentDate(new Date());
            m.setContent(text);
            Transport.send(m);
		 out.println("Mail sent....");
      }
      catch(Exception e)
      {
         out.println(e.toString());
      }
      finally
      {
         out.println("</BODY></HTML>");
         out.close();
      }