Java Reference
In-Depth Information
L
ISTING
8.3
Continued
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Create some properties and get the default Session.
String host = “YOURMAILHOST”;
String to = request.getParameter(“to”);
String from = request.getParameter(“from”);
String subject = request.getParameter(“subject”);
String messageText = request.getParameter(“body”);
boolean sessionDebug = false;
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
8
out.println(“<html>”);
out.println(“<head><title>MailServlet</title></head>”);
Properties props = System.getProperties();
props.put(“mail.host”, host);
props.put(“mail.transport.protocol”, “smtp”);
Session mailSession = Session.getDefaultInstance(props, null);
// Set debug on the Session so we can see what is going on
// Passing false will not echo debug info, and passing true
// will.
mailSession.setDebug(sessionDebug);
// Instantiate a new MimeMessage and fill it with the
// required information.
Message msg = new MimeMessage(mailSession);
try {
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);