A: condition must download sun's JavaMail API package, address: http://java.sun.com/products/javamail/
I use version 1.2 here and add the relevant package (jar file) to CLASSPATH.
II. This program is very simple, and we don't need to consider a lot of ground layer things, because the API has helped us do these things well. Below is a simple e-mail Servlet: (for people familiar with it, i'm afraid it's a simple servlet)
Import java. io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import sun.net. smtp .*;
Public class SendMailServlet extends HttpServlet {
Public static String MAIL_FROM = "from ";
Public static String MAIL_TO = "";
Public static String MAIL_SUBJECT = "subject ";
Public static String MAIL_BODY = "body ";
Public static String MAIL_HOST = "mailhost ";
Public void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
Resp. setContentType ("text/html; charset = gb2312 ");
PrintWriter out = resp. getWriter ();
Out. println ("<form method = POST action =" "+ req. getRequestURI () +" "> ");
Out. println ("<table> ");
Out. println ("<tr> <td> send mail server: </td> ");
Out. println ("<td> <input type = text name =" + MAIL_HOST + "size = 30> </td> </tr> ");
Out. println ("<tr> <td> from: </td> ");
Out. println ("<td> <input type = text name =" + MAIL_FROM + "size = 30> </td> </tr> ");
Out. println ("<tr> <td> to: </td> ");
Out. println ("<td> <input type = text name =" + MAIL_TO + "size = 30> </td> </tr> ");
Out. println ("<tr> <td> subject: </td> ");
Out. println ("<td> <input type = text name =" + MAIL_SUBJECT + "size = 30> </td> </tr> ");
Out. println ("<tr> <td> text: </td> ");
Out. println ("<td> <textarea name =" + MAIL_BODY + "cols = 40 rows = 10> </textarea> </td> </tr> ");
Out. println ("</table> <br> ");
Out. println ("<input type = submit value =" Send "> ");
Out. println ("<input type = reset value =" Reset "> ");
Out. println ("</form> ");
Out. flush ();
}
Public void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
Resp. setContentType ("text/html; charset = gb2312 ");
PrintWriter out = new PrintWriter (resp. getOutputStream ());
String from = req. getParameter (MAIL_FROM );
String to = req. getParameter (MAIL_TO );
String subject = req. getParameter (MAIL_SUBJECT );
String body = req. getParameter (MAIL_BODY );
String mailhost = req. getParameter (MAIL_HOST );
Try
{
SmtpClient mailer = new SmtpClient (mailhost );
Mailer. from (from );
Mailer. to ();
PrintStream ps = mailer. startMessage ();
Ps. println ("From:" + from );
Ps. println ("To:" + );
Ps. println ("Subject:" + subject );
Ps. println (body );
Mailer. closeServer ();
Out. println ("Success! ");
}
Catch (Exception ex)
{
Out. println ("An error about:" + ex. getMessage ());
}
Out. flush ();
}
Public void init (ServletConfig cfg) throws ServletException
{
Super. init (cfg );
}
Public void destroy ()
{
Super. destroy ();
}
}