| A class to help send SMTP email. It can be used by any Java program, not
just servlets. Servlets are likely to use this class to:
- Send submitted form data to interested parties
- Send an email page to an administrator in case of error
- Send the client an order confirmation
This class is an improvement on the sun.net.smtp.SmtpClient class
found in the JDK. This version has extra functionality, and can be used
with JVMs that did not extend from the JDK. It's not as robust as
the JavaMail Standard Extension classes, but it's easier to use and
easier to install.
It can be used like this:
String mailhost = "localhost"; // or another mail host
String from = "Mail Message Servlet ";
String to = "to@somedomain.com";
String cc1 = "cc1@somedomain.com";
String cc2 = "cc2@somedomain.com";
String bcc = "bcc@somedomain.com";
MailMessage msg = new MailMessage(mailhost);
msg.from(from);
msg.to(to);
msg.cc(cc1);
msg.cc(cc2);
msg.bcc(bcc);
msg.setSubject("Test subject");
PrintStream out = msg.getPrintStream();
Enumeration paramenum = req.getParameterNames();
while (paramenum.hasMoreElements()) {
String name = (String)paramenum.nextElement();
String value = req.getParameter(name);
out.println(name + " = " + value);
}
msg.sendAndClose();
Be sure to set the from address, then set the recepient
addresses, then set the subject and other headers, then get the
PrintStream, then write the message, and finally send and close.
The class does minimal error checking internally; it counts on the mail
host to complain if there's any malformatted input or out of order
execution.
An attachment mechanism based on RFC 1521 could be implemented on top of
this class. In the meanwhile, JavaMail is the best solution for sending
email with attachments.
Still to do:
- Figure out how to close the connection in case of error
author: Jason Hunter, Copyright © 1999 version: 1.2, 2002/11/01, added logic to suppress CC: header if no CC addrs version: 1.1, 2000/03/19, added angle brackets to address, helps some servers version: 1.0, 1999/12/29 |