亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? dlog_mailsenderservlet.java

?? 個人Blog java編寫的Blog可以直接使用!
?? JAVA
字號:
/*
 *  DLOG_MailSenderServlet.java
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *  
 *  Author: Winter Lau
 *  http://dlog4j.sourceforge.net
 *  
 */
package com.liusoft.dlog4j.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.Type;

import com.liusoft.dlog4j.Globals;
import com.liusoft.dlog4j.MailTransportQueue;
import com.liusoft.dlog4j.util.StringUtils;

/**
 * 郵件傳遞服務程序
 * @author Winter Lau
 */
public class DLOG_MailSenderServlet extends GenericServlet implements Runnable{

	protected String sender;
	protected String smtp_host;
	protected int smtp_port = 25;
	protected String smtp_user;
	protected String smtp_pass;
	protected String mail_queue_path;
	
	private boolean userDNSQuery = true;
	private boolean stop = false;
	private Thread tMailSender;
	
	private MailTransportQueue queue = null;
	
	private Session mailSession;
	
	/**
	 * 初始化配置信息
	 */
	public void init() throws ServletException {
		super.init();
		
		this.initParams();
		
		if(StringUtils.isNotEmpty(smtp_host)){
			userDNSQuery = false;
	        Properties mailProperties = System.getProperties();
	        mailProperties.put("mail.smtp.host", smtp_host);
	        if(smtp_port>0 && smtp_port!=25)
	            mailProperties.put("mail.smtp.port", String.valueOf(smtp_port));
	        mailProperties.put("mail.smtp.auth", "true"); //設置smtp認證,很關鍵的一句
	        mailSession = Session.getDefaultInstance(mailProperties, new Authenticator(){
				protected PasswordAuthentication getPasswordAuthentication() {
			        return new PasswordAuthentication(smtp_user, smtp_pass);
				}});
		}
		else{
	        Properties props = new Properties();
	        //Not needed for production environment
	        props.put("mail.debug", "false");
	        //Prevents problems encountered with 250 OK Messages
	        props.put("mail.smtp.ehlo", "false");
	        //Sets timeout on going connections
	        props.put("mail.smtp.timeout", smtpTimeout + "");

	        props.put("mail.smtp.connectiontimeout", connectionTimeout + "");
	        props.put("mail.smtp.sendpartial",String.valueOf(sendPartial));

	        mailSession = Session.getInstance(props, null);
		}
		
		tMailSender = new Thread(this);
		tMailSender.start();
	}
	
	/**
	 * 從配置中讀取信息
	 * @throws ServletException
	 */
	protected void initParams() throws ServletException{
		sender = getInitParameter("sender");
		if(StringUtils.isEmpty(sender))
			throw new ServletException("Parameter sender is required.");
		mail_queue_path = getInitParameter("mail-queue-path");
		if(StringUtils.isEmpty(mail_queue_path))
			throw new ServletException("Parameter mail-queue-path is required.");
		else{
			if(mail_queue_path.startsWith(Globals.LOCAL_PATH_PREFIX)){
				mail_queue_path = mail_queue_path.substring(Globals.LOCAL_PATH_PREFIX.length());
			}
			else if(mail_queue_path.startsWith("/")){
				mail_queue_path = getServletContext().getRealPath(mail_queue_path);
			}
			if(!mail_queue_path.endsWith(File.separator))
				mail_queue_path += File.separator;
			queue = MailTransportQueue.getInstance(mail_queue_path);
			getServletContext().setAttribute(Globals.MAIL_QUEUE, queue);
		}
		smtp_host = getInitParameter("smtp-host");
		String tmp = getInitParameter("smtp-port");
		if(StringUtils.isNotEmpty(tmp) && StringUtils.isNumeric(tmp))
			smtp_port = Integer.parseInt(tmp);
		smtp_user = getInitParameter("smtp-user");
		smtp_pass = getInitParameter("smtp-password");
	}

	/**
	 * 郵件傳送線程入口
	 */
	public void run(){		
		while(!stop){
			long timeToSleep = 1000;
			List mails = new ArrayList();
			int i = 0;
			try{
				int mailc = queue.read(mailSession, mails, null, 1);
				if(mailc > 1)
					timeToSleep = 0;
				if(mails.size()>0){
					for(;i<mails.size();i++){
						MimeMessage mail = (MimeMessage)mails.get(i);
						mail.setFrom(new InternetAddress(sender,"DLOG4J Messenger"));
						if(userDNSQuery){
							//特快專遞
							String email = mail.getRecipients(RecipientType.TO)[0].toString();
							String domain_name = parseDomain(email);
							//TODO: 實現域名的緩存,加快解析速度
							Lookup lookup = new Lookup(domain_name, Type.MX);
					        lookup.run();
							if (lookup.getResult() != Lookup.SUCCESSFUL){
								log("ERROR: " + lookup.getErrorString() + " when lookup MX record of " + email);
								continue;
							}
							Record[] answers = lookup.getAnswers();
							for(int ai=0;ai<answers.length;ai++){
								Transport transport = null;
						        log("Using " + answers[i].getAdditionalName()+" to send mail to " + email);
						        String mx_host = answers[i].getAdditionalName().toString();
						        mailSession.getProperties().put("mail.smtp.host", mx_host);
						        InternetAddress smtp_host = new InternetAddress(mx_host);
						        try {
						            transport = mailSession.getTransport(smtp_host);
						            try {
						                transport.connect();
						                log("INFO: connected to "+mx_host);
						            } catch (MessagingException me) {
						                // Any error on connect should cause the mailet to attempt
						                // to connect to the next SMTP server associated with this
						                // MX record.  Just log the exception.  We'll worry about
						                // failing the message at the end of the loop.
						                me.printStackTrace(); 
						                log("ERROR: Connecto to " + mx_host + " failed." , me);
						                continue;
						            }
						            InternetAddress mailToAddress = new InternetAddress(email);            
						            transport.sendMessage(mail, new InternetAddress[]{mailToAddress});
						            log("INFO: mail sent to " + email);
						            break;
						        } finally {
						            if (transport != null) {
						                transport.close();
						                transport = null;
						            }
						        }
							}
						}
						else{
							//使用指定郵件帳號發送
							Transport.send(mail);
						}
					}
				}
			}catch(Exception e){
				log("ERROR: 郵件傳送失敗,詳細信息如下,", e);
			}finally{
				mails = null;
				if(timeToSleep > 0){
					try {
						Thread.sleep(timeToSleep);
					} catch (InterruptedException e) {}
				}
			}
		}
	}

	/* (non-Javadoc)
	 * @see javax.servlet.GenericServlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
	 */
	public void service(ServletRequest req, ServletResponse res)
			throws ServletException, IOException {
		//TODO: 監控輸出
	}

	/**
	 * 停止郵件傳送線程
	 */
	public void destroy() {
		stop = true;
		if(tMailSender!=null){
			try {
				tMailSender.join(10000, 100);
			} catch (InterruptedException e) {
			}
		}
		super.destroy();
	}

	/**
	 * 從郵件地址中解析出域名
	 * @param email
	 * @return
	 */
	private static String parseDomain(String email){
		String domain = null;
		if(email!=null){
			int idx = email.indexOf('@');
			if(idx != -1){
				idx++;
				if(idx<email.length())
					domain = email.substring(idx);
			}
		}
		return domain;
	}
	
    private static long smtpTimeout = 600000;  //default number of ms to timeout on smtp delivery
    private static int connectionTimeout = 60000;  // The amount of time JavaMail will wait before giving up on a socket connect()
    private static boolean sendPartial = false; // If false then ANY address errors will cause the transmission to fail
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产精品国产三级国产aⅴ中文 | 国产综合色产在线精品| 国产福利一区二区三区视频| 色天天综合色天天久久| 日韩精品一区二区三区在线播放 | 日本一区二区成人| 日韩专区一卡二卡| 91视频你懂的| 国产人成亚洲第一网站在线播放| 亚洲va欧美va人人爽午夜| 国产成人av电影在线播放| 欧美一区二区三区色| 亚洲视频一区二区免费在线观看| 国产精品一区二区久久精品爱涩| 欧美一区二区三区在线视频| 亚洲自拍欧美精品| 91在线一区二区三区| 亚洲国产成人午夜在线一区| 国产乱码精品一区二区三区忘忧草| 日本韩国欧美国产| 亚洲欧美另类综合偷拍| 处破女av一区二区| 亚洲国产精品成人综合| 国产福利一区二区| 国产精品人妖ts系列视频| 国产精品综合在线视频| 久久免费美女视频| 久久99国产精品久久| 欧美成人在线直播| 国产一区亚洲一区| 久久久精品tv| 国产.欧美.日韩| 亚洲国产精品成人综合色在线婷婷| 国产精品一区二区在线播放| 久久香蕉国产线看观看99| 国产精品一二三区在线| 国产精品网站导航| 色系网站成人免费| 亚洲成人av电影在线| 91.麻豆视频| 韩国女主播一区| 26uuuu精品一区二区| 国产乱妇无码大片在线观看| 国产人伦精品一区二区| 99热精品一区二区| 亚洲激情图片一区| 欧美日韩视频在线一区二区| 视频在线观看一区| 久久久五月婷婷| 91在线高清观看| 亚洲丶国产丶欧美一区二区三区| 欧美一区二区三区免费视频| 精一区二区三区| 国产精品久久久一本精品| 99re热这里只有精品免费视频| 亚洲成人动漫在线观看| 精品国产乱码久久久久久浪潮 | 亚洲国产成人一区二区三区| 在线看不卡av| 精品系列免费在线观看| 中文字幕一区二区三中文字幕| 色av综合在线| 日本91福利区| 亚洲人被黑人高潮完整版| 91精品国模一区二区三区| 国产剧情在线观看一区二区| 一区二区免费在线播放| 久久综合视频网| 欧美这里有精品| 国产自产高清不卡| 亚洲第一激情av| 欧美激情一区二区| 4438x亚洲最大成人网| 成人免费精品视频| 青草国产精品久久久久久| 国产精品国产自产拍在线| 欧美一级在线免费| 一本一道综合狠狠老| 国内精品久久久久影院薰衣草| 亚洲精品成人在线| 日本一区二区三区免费乱视频| 欧美人与禽zozo性伦| 成人aaaa免费全部观看| 日本不卡不码高清免费观看| 亚洲欧美日韩国产综合| 久久精品免视看| 精品欧美一区二区久久| 欧美午夜精品久久久久久孕妇| 国产aⅴ精品一区二区三区色成熟| 无码av免费一区二区三区试看| 国产精品区一区二区三| 2017欧美狠狠色| 欧美高清www午色夜在线视频| 99久久伊人网影院| 国产精品亚洲第一区在线暖暖韩国| 日韩精品视频网| 亚洲国产视频a| 伊人色综合久久天天| 日韩一区在线看| 国产精品国产馆在线真实露脸| 久久色视频免费观看| 日韩午夜在线观看视频| 777a∨成人精品桃花网| 欧美人成免费网站| 精品1区2区3区| 欧美性受xxxx黑人xyx性爽| 91在线无精精品入口| 99re成人精品视频| 成人免费高清在线| 成人永久aaa| 成人一区在线观看| 成人福利视频在线| 93久久精品日日躁夜夜躁欧美| 成人av在线资源网站| 99久久久无码国产精品| 东方aⅴ免费观看久久av| 国产精品影视网| 国产精品1区二区.| 成人动漫精品一区二区| 风流少妇一区二区| 99在线视频精品| 在线免费观看成人短视频| 欧美日韩精品免费观看视频| 欧美老年两性高潮| 91精品国产91综合久久蜜臀| 日韩一区二区三区视频| 精品国产三级电影在线观看| 久久精品免视看| 亚洲人成网站影音先锋播放| 亚洲国产一区在线观看| 奇米影视在线99精品| 国内精品伊人久久久久av一坑| 国产精品一二三区在线| 91在线视频18| 欧美日韩日日夜夜| 久久亚洲春色中文字幕久久久| 中文字幕在线一区免费| 亚洲一区二区三区三| 美女视频网站久久| 国产成人在线看| 欧洲精品在线观看| 日韩精品一区二区在线| 国产精品福利一区二区| 亚洲午夜电影在线观看| 激情综合亚洲精品| 色女孩综合影院| 精品美女一区二区| 中文字幕日韩精品一区| 午夜精品久久久久久久久久久| 激情av综合网| 欧美在线视频不卡| 精品99一区二区三区| 亚洲精品亚洲人成人网| 麻豆精品视频在线观看视频| 成人av资源在线观看| 日韩三级电影网址| 亚洲日本成人在线观看| 男人操女人的视频在线观看欧美| 国产成人av网站| 日韩限制级电影在线观看| 亚洲欧洲成人自拍| 久久99精品国产| 欧美日韩在线三级| 国产精品国产馆在线真实露脸| 精品一区二区免费| 欧美日韩一区久久| 中文字幕第一区| 奇米亚洲午夜久久精品| 91国产精品成人| 中文乱码免费一区二区| 蜜桃一区二区三区在线观看| 91色porny| 亚洲国产精品成人综合| 裸体在线国模精品偷拍| 欧美在线制服丝袜| 亚洲色图一区二区三区| 国产精品一区二区无线| 日韩欧美国产一区二区在线播放 | 成人久久视频在线观看| 日韩欧美在线影院| 亚洲国产精品久久人人爱| 成人sese在线| 国产欧美日韩在线看| 国内一区二区视频| 日韩欧美国产小视频| 婷婷久久综合九色综合伊人色| 一本大道久久a久久综合| 国产精品久久久久久久久免费桃花| 国产在线精品一区二区三区不卡 | 黄色日韩网站视频| 日韩一区二区三区免费看 | 亚洲精品高清在线| 99久久精品久久久久久清纯| 欧美经典一区二区三区| 国产成人在线视频网站| 中文字幕欧美三区| 99在线视频精品| 亚洲一区二区四区蜜桃| 欧美在线999| 午夜激情久久久|