• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

JAVA/JSP学习系列之十(JavaMail API发邮

servlet/jsp 搞代码 7年前 (2018-06-18) 141次浏览 已收录 0个评论

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

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

import java.io.*;

http://www.gaodaima.com/42317.htmlJAVA/jsP学习系列之十(JavaMail API发邮

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

}

}

欢迎大家阅读《JAVA/jsp学习系列之十(JavaMail API发邮》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:JAVA/JSP学习系列之十(JavaMail API发邮

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址