Java Reference
In-Depth Information
L
ISTING
8.3
Continued
msg.setSentDate(new Date());
msg.setText(messageText);
// Hand the message to the default transport service
// for delivery.
Transport.send(msg);
out.println(“<body>”);
out.println(“Mail was sent to “ + to);
out.println(“ from “ + from);
out.println(“ using host “ + host + “.”);
out.println(“</body></html>”);
}
catch (MessagingException mex) {
mex.printStackTrace();
}
finally {
out.close();
}
}
}
As you look over the
MailServlet
, you will notice only a few differences from the
JavaMail
application in Listing 8.1. The first change is the addition of the code to get the necessary
request parameters, which is included in the following snippet:
String to = request.getParameter(“to”);
String from = request.getParameter(“from”);
String subject = request.getParameter(“subject”);
String messageText = request.getParameter(“body”);
The only other notable change is that, instead of referring to the Session with the variable
name
session
, we have changed the variable name to
mailSession
.
To see this example in action, copy the HTML file and the
MailServlet
into your
<SERVER_ROOT>/djs/
Web application and load the following URL into your browser:
http://
localhost
/djs/MailForm.html
You should see a page similar to Figure 8.1.