- java如何實現(xiàn)后臺自動發(fā)郵件功能 推薦度:
- 相關(guān)推薦
java實現(xiàn)后臺自動發(fā)郵件功能
java后臺自動發(fā)郵件功能怎樣實現(xiàn)呢?下面是小編帶來的解決方法,希望對您有所幫助!
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><!--郵件發(fā)送程序-->
<servlet-class>com.email.MailSenderServlet</servlet-class>
<init-param>
<param-name>server</param-name><!--郵件服務(wù)器SMTP地址-->
<param-value>smtp.163.com</param-value>
</init-param>
<init-param>
<param-name>port</param-name><!--郵件服務(wù)器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服務(wù)器是否要求驗證-->
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>time</param-name><!--掃描數(shù)據(jù)庫時間間隔(分鐘)-->
<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("系統(tǒng)文件web.xml錯誤:郵件發(fā)送程序初始化失敗!");
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(); //啟動郵件發(fā)送線程
}
public void destroy() {
sender.stop(); //停止郵件發(fā)送線程
try {
t.join(1000);
if (t.isAlive()) {
System.out.println("郵件發(fā)送線程未停止。");
}
}
catch (Exception e) {}
}
}
郵件發(fā)送程序
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>
* @author flyxxxxx@163.com
* @version 1.0
*/
public class MailSender
implements Runnable {
private int time = 5 * 60 * 1000; //掃描數(shù)據(jù)庫時間間隔
private boolean flag = true; //停止線程標記
private String server = "127.0.0.1"; //SMTP服務(wù)器地址
private String port = "25"; //SMTP服務(wù)器端口
private String address; //用于發(fā)送EMAIL的發(fā)送者地址
private String username; //發(fā)送者的用戶名
private String password; //發(fā)送者的密碼
private boolean validate = true; //郵件服務(wù)器是否要求驗證
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(); //保存前一次發(fā)送郵件的時間
while (flag) { //服務(wù)器停止時退出線程
long k = new Date().getTime() - lastTime;
if (k < -1000) { //防止系統(tǒng)修改時間
lastTime = new Date().getTime();
continue;
}
if (k > time) { //超過設(shè)定時間間隔開始發(fā)送郵件
sendData();
lastTime = new Date().getTime();
}
try {
Thread.sleep(100);
}
catch (Exception e) {}
}
}
public void stop() {
flag = false;
}
/**
* 檢索數(shù)據(jù)庫,并發(fā)送郵件
*/
private void sendData() {
ResultSet rs=null;//讀取數(shù)據(jù)庫數(shù)據(jù)
try {
Session session = Session.getInstance(getProperties(), new Authentic());
while(flag) {//服務(wù)器停止時退出線程
String toAddress = null;//發(fā)送地址(從rs中得到)
String subject = null;//郵件主題
String content = null ;//郵件內(nèi)容
String file[] = null;//所有附件(絕對路徑)
sendMail(session, toAddress, subject, content, file);//發(fā)送郵件
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if(rs!=null){
try{
rs.close();
}
catch(SQLException e){}
}
}
}
/**
* 發(fā)送郵件
* @param session 會話
* @param toAddress 目的地址
* @param subject 郵件主題
* @param content 郵件內(nèi)容(HTML)
* @param files 郵件附件
* @return 是否發(fā)送成功
*/
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); //發(fā)送地址
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內(nèi)容
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); //郵件全部內(nèi)容
rs.setSentDate(new Date()); //發(fā)送時間
Transport.send(rs); //發(fā)送
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);
}
}
}
更多java相關(guān)文章推薦:
3.新手學(xué)習(xí)Java語言六大關(guān)鍵
11.Java語言學(xué)習(xí)的主要內(nèi)容
【java實現(xiàn)后臺自動發(fā)郵件功能】相關(guān)文章:
java如何實現(xiàn)后臺自動發(fā)郵件功能09-17
如何運用Java socket實現(xiàn)多人聊天室功能11-06
如何使用Java實現(xiàn)AC自動機全文檢索實例10-24
Java 隊列實現(xiàn)原理及簡單實現(xiàn)代碼09-13
Java實現(xiàn)多繼承的實例07-18
權(quán)重隨機算法的java實現(xiàn)08-13
關(guān)于Java動態(tài)實現(xiàn)的方法08-23
java圖片處理功能介紹06-23