Sending email from a Servlet

This example shows you how to send an email from a Servlet using JavaMail 1.2. Donwload the latest version of JavaMail at http://java.sun.com/products/javamail/index.html. Try it out with the following HTML file, SendMailServlet.java and SendMail.java. You can try out SendMail.java as a stand-alone application.
SendMailServlet.java:

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class SendMailServlet extends HttpServlet
{
   private HttpServletRequest req = null;
   PrintWriter out;
 
   public void doPost (HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
 
      this.req = req;
      res.setContentType("text/html");
      out = res.getWriter();
 
      try {
         String smtpHost = getParameter("smtphost");
         String from = getParameter("from");
         String to = getParameter("to");
         String subject = getParameter("subject");
         String message = getParameter("message");
 
         out.println("<html><body>");
   
         try {
            SendMail sm = new SendMail(smtpHost);
            sm.mailto(from, to, subject, message);
            out.println("Mail sent!");
         } catch(Exception e) {
            out.println("<br><br>" + e);
         }
 
         out.println("</body></html>");
      }
      catch(Exception e) {
         out.println("<HTML><BODY>" + e + "</BODY></HTML>");
         e.printStackTrace(out);
      }
 
      out.close();
   }
 
   public String getParameter(String parameter) {
      String values[] = req.getParameterValues(parameter);
 
      if (values == null) return "";
      if (values.length == 0) return "";
      return values[0];
   }
}

SendMail.java:

import javax.mail.*; 
import javax.mail.internet.*; 
 
public class SendMail
{ 
   Session session;
 
   public SendMail(String smtpHost) throws Exception {
      java.util.Properties properties = System.getProperties(); 
      properties.put("mail.smtp.host", smtpHost); 
      session = Session.getInstance(properties,null);
   }
 
   public void mailto(String from, String to, String subject, String message) throws Exception
   { 
         MimeMessage mimeMessage = new MimeMessage(session); 
         mimeMessage.setFrom(new InternetAddress(from)); 
         mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
         mimeMessage.setSubject(subject); 
         mimeMessage.setText(message); 
 
         Transport.send(mimeMessage); 
   } 
 
   public static void main(String []args) throws Exception {
      System.out.print("Sending mail... ");
      SendMail sm = new SendMail("your smtp server");
      sm.mailto("from field","to field", "subject field", "message field");
      System.out.println("Sent!");
   }
}

test.html:

<html>
<body>
<form METHOD=POST ACTION="http://www.esus.com/servlets/SendMailServlet">
SMTP Host:<br>
<dd><input TYPE=TEXT NAME="smtphost"><p>
From:<br>
<dd><input TYPE=TEXT NAME="from"><p>
To:<br>
<dd><input TYPE=TEXT NAME="to"><p>
Subject:<br> 
<dd><input TYPE=TEXT NAME="subject"><p>
Message:<br>
<dd><textarea name="message" cols=60 rows=4>
<br>
<input TYPE=SUBMIT VALUE="Send!">
</form>
</body>
</html>