JAVA/JSP Learning Series ten (JavaMail API email [servlet])

Source: Internet
Author: User
Tags flush mail reset smtpclient
js|servlet| email I'm here with version 1.2, add the related package (jar file) to the Classpath

Second: The program is very simple, do not need us to consider a lot of strata of things, because the API to help us do these things, the following is a Simple mail servlet: (for familiar people, I am afraid is 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 = ' 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 (to);

PrintStream PS = Mailer.startmessage ();

Ps.println ("From:" + from);

Ps.println ("to:" + 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 ();

}

}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.