web.xml文件
http://java.sun.com/dtd/web-app_2_3.dtd">
mailsenderservlet
com.email.MailSenderServlet
server
smtp.163.com
port
25
address
username@163.com
username
username
password
******
validate
true
time
5
1
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.*;
/**
*
Copyright: Copyright (c) 2004
* @author
flyxxxxx@163.com * @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) {
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);
}
}
}
posted on 2005-08-18 13:13
my java 阅读(1450)
评论(1) 编辑 收藏 所属分类:
java mail