JAVA/JSP學習系列之十(JavaMail API發郵件[servlet])

來源:互聯網
上載者:User
一:條件 必須下載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();

}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.