« | October 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | |
|
统计 |
blog名称:小雨 日志总数:262 评论数量:1273 留言数量:15 访问次数:4686040 建立时间:2005年1月8日 |
| 
|
W3CHINA Blog首页 管理页面 写新日志 退出
[知识积累]jsp 发邮件 |
小雨 发表于 2006/12/14 23:37:05 | 很简单的东西 ,总有人问,所以贴出来
package zj;import java.io.*;import javax.mail.*; import javax.mail.internet.*;
import javax.servlet.*;import java.util.*; import javax.servlet.http.*;
import sun.net.smtp.*;
public class test extends HttpServlet { static class SmtpAuth extends javax.mail.Authenticator { private String user,password;
public void getuserinfo(String getuser,String getpassword){ user = getuser; password = getpassword; } protected javax.mail.PasswordAuthentication getPasswordAuthentication(){ return new javax.mail.PasswordAuthentication(user,password); } } 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"); 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); PrintWriter out = new PrintWriter(resp.getOutputStream()); try{SmtpAuth sa=new SmtpAuth();
sa.getuserinfo("username","pwd"); java.util.Properties props=new java.util.Properties(); props.put("mail.smtp.auth","true"); props.put("mail.smtp.host",mailhost); Session mailSession=Session.getDefaultInstance(props,sa); MimeMessage testMessage=new MimeMessage(mailSession); testMessage.setFrom(new InternetAddress(from)); testMessage.addRecipient(javax.mail.Message.RecipientType.TO,new InternetAddress(to)); testMessage.setSentDate(new java.util.Date()); testMessage.setSubject(subject); testMessage.setText(body); System.out.println("Message constructed");
Transport.send(testMessage);
}
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();
}
}
|
阅读全文(2321) | 回复(0) | 编辑 | 精华 |
|