教育改变生活,科泰成就梦想!
免费热线:400-696-8028

教学课堂CLASSROOM

开班信息
热门话题
java技术
目前所在位置: 首页 > java技术

JAVA-JSP学习系列之十(JavaMail API发邮件[servlet])

来源:北大青鸟科泰校区时间:2019-04-08

内容提要: 北大青鸟科泰IT培训学院提供JAVA-JSP学习系列之十(JavaMail API发邮件[servlet])

一:条件 必须下载sun公司的JavaMail API包,地址为:http://java.sun.com/products/javamail/

我这里用的是1.2版本,将相关包(jar文件)加到CLASSPATH中

二:该程序非常简单,不需要我们考虑很多地层的东西,因为API都帮我们做好了这些事情,下面是*个简单的发邮件的Servlet:(对于熟悉的人来说,恐怕是再简单不过了的*个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(); 

}


联系我们

扫码关注科泰

免费咨询:400-696-8028

报名电话:0731-88122069

咨询QQ: 783463085

学校地址:长沙市开福区湘江北路与开顺路交汇处(金霞跨境产贸城)

在线报名

长沙北大青鸟科泰IT梦想学院·长沙科泰电子商务有限公司·版权所有__湘ICP备19001789号-1