web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>mailsenderservlet</servlet-name><!--邮件发送程序 --> <servlet-class>com.email.MailSenderServlet</servlet-class> <init-param> <param-name>server</param-name><!--邮件服务器SMTP地址--> <param-value>smtp.163.com</param-value> </init-param> <init-param> <param-name>port</param-name><!--邮件服务器SMTP端口--> <param-value>25</param-value> </init-param> <init-param> <param-name>address</param-name><!--邮件地址--> <param-value>username@163.com</param-value> </init-param> <init-param> <param-name>username</param-name><!--邮件账号--> <param-value>username</param-value> </init-param> <init-param> <param-name>password</param-name><!--密码--> <param-value>******</param-value> </init-param> <init-param> <param-name>validate</param-name><!--SMTP服务器是否要求验证--> <param-value>true</param-value> </init-param> <init-param> <param-name>time</param-name><!--扫描数据库时间间隔(分钟)--> <param-value>5</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> </web-app> servlet: package com.email; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class MailSenderServlet extends HttpServlet { private MailSender sender; private Thread t; public void init() throws ServletException { sender = new MailSender(); sender.setRoot(root); String server = getInitParameter("server"); String port = getInitParameter("port"); String address = getInitParameter("address"); String username = getInitParameter("username"); String password = getInitParameter("password"); if (server == null || port == null || address == null || username == null || password == null) { System.out.println("系统文件web.xml错误:邮件发送程序初始化失败!"); return; } sender.setServer(server); sender.setPort(port); sender.setAddress(address); sender.setUsername(username); sender.setPassword(password); String time = getInitParameter("time"); if (time != null) { sender.setTime(Integer.parseInt(time)); } String validate = getInitParameter("validate"); if (validate != null) { sender.setValidate(Boolean.valueOf(validate).booleanValue()); } t = new Thread(sender); t.start(); //启动邮件发送线程 } public void destroy() { sender.stop(); //停止邮件发送线程 try { t.join(1000); if (t.isAlive()) { System.out.println("邮件发送线程未停止。"); } } catch (Exception e) {} } } 邮件发送程序 package com.email; import java.io.*; import java.sql.*; import java.util.*; import java.util.Date; import javax.activation.*; import javax.mail.*; import javax.mail.Message.*; import javax.mail.internet.*; /** * <p>Copyright: Copyright (c) 2004</p> * * @version 1.0 */ public class MailSender implements Runnable { private int time = 5 * 60 * 1000; //扫描数据库时间间隔 private boolean flag = true; //停止线程标记 private String server = "127.0.0.1"; //SMTP服务器地址 private String port = "25"; //SMTP服务器端口 private String address; //用于发送EMAIL的发送者地址 private String username; //发送者的用户名 private String password; //发送者的密码 private boolean validate = true; //邮件服务器是否要求验证 private File root = null; //WEB根目录 MailConfigManager manager = MailConfigManager.getInstance(); public MailSender() { } public void setAddress(String address) { this.address = address; } public void setPassword(String password) { this.password = password; } public void setPort(String port) { if (port != null && port.length() > 0) { this.port = port; } } public void setServer(String server) { this.server = server; } public void setUsername(String username) { this.username = username; } public void setValidate(boolean validate) { this.validate = validate; } public void setTime(int minute) { this.time = minute * 60 * 1000; } public void run() { long lastTime = new Date().getTime(); //保存前一次发送邮件的时间 while (flag) { //服务器停止时退出线程 long k = new Date().getTime() - lastTime; if (k < -1000) { //防止系统修改时间 lastTime = new Date().getTime(); continue; } if (k > time) { //超过设定时间间隔开始发送邮件 sendData(); lastTime = new Date().getTime(); } try { Thread.sleep(100); } catch (Exception e) {} } } public void stop() { flag = false; } /** * 检索数据库,并发送邮件 */ private void sendData() { ResultSet rs=null;//读取数据库数据 try { Session session = Session.getInstance(getProperties(), new Authentic()); while(flag) {//服务器停止时退出线程 String toAddress = null;//发送地址(从rs中得到) String subject = null;//邮件主题 String content = null ;//邮件内容 String file[] = null;//所有附件(绝对路径) sendMail(session, toAddress, subject, content, file);//发送邮件 } } catch (Exception e) { e.printStackTrace(); } finally { if(rs!=null){ try{ rs.close(); } catch(SQLException e){} } } } /** * 发送邮件 * @param session 会话 * @param toAddress 目的地址 * @param subject 邮件主题 * @param content 邮件内容(HTML) * @param files 邮件附件 * @return 是否发送成功 */ private boolean sendMail(Session session, String toAddress, String subject, String content, String[] files) { toAddress = "lijin@regaltec.com.cn"; try { Message rs = new MimeMessage(session); Address from = new InternetAddress(address); rs.setFrom(from); //发送地址 rs.setRecipient(RecipientType.TO, new InternetAddress(toAddress)); //接收地址 rs.setSubject(subject); //邮件主题 Multipart mp = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(content, "text/html; charset=GBK"); //邮件HTML内容 mp.addBodyPart(html); if (files != null && files.length > 0) { //邮件附件 for (int i = 0; i < files.length; i++) { MimeBodyPart mbp = new MimeBodyPart(); FileDataSource fds = new FileDataSource(files[i]); mbp.setDataHandler(new DataHandler(fds)); mbp.setFileName(MimeUtility.encodeWord(files[i]), "GBK", null)); mp.addBodyPart(mbp); } } rs.setContent(mp); //邮件全部内容 rs.setSentDate(new Date()); //发送时间 Transport.send(rs); //发送 return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 邮件会话属性 * @return 会话属性 */ private Properties getProperties() { Properties rs = new Properties(); rs.put("mail.smtp.host", server); rs.put("mail.smtp.port", port); rs.put("mail.smtp.auth", validate ? "true" : "false"); return rs; } public void setRoot(File root) { this.root = root; } class Authentic extends Authenticator { //验证密码 public Authentic() { } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } } }