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 ();
}
}