web.xml文件
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) {
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);
}
}
}
该文章转载自[编程助理站]:http://www.91duoduo.com/jishu/java_xx.html?page=1&noid=ffhjbi&t=