YxySendSmtpInfoAction.java 9.3 KB
package com.espeed.action;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.Transport;

import com.espeed.centre.pojo.YxyUserInfo;
import com.espeed.log.LogClass;
import com.espeed.pojo.YxySendSmtpInfo;
import com.espeed.pojo.YxySmtpList;
import com.espeed.service.YxySendSmtpInfoService;
import com.espeed.tool.DES_Encrypt;
import com.opensymphony.xwork2.ActionContext;

/***
 * SMTP信息action
 */
public class YxySendSmtpInfoAction extends BaseAction{
	private static final long serialVersionUID = 1L;

	/**查询用户SMTP信息*/
	public String findAllSmtp(){
		try {
			//获取用户session
			ActionContext ac = ActionContext.getContext();
			Map<String,Object> sess = ac.getSession();			
			YxyUserInfo user=(YxyUserInfo) sess.get("yxyuser");
			loginid=user.getLogin_id();//用户账号
			domain=user.getDomain();//用户所属域名
			smtplist=yxysendsmtpinfoservice.findAllSmtp(loginid,domain);
		} catch (Exception e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
	        String str = sw.toString();
	        LogClass.errolog(str,loginid+"@"+domain);
		}
		return "list";
	}
	
	/**查询SMTP List信息*/
	public String findSmtpList(){
		try {
			//获取用户session
			ActionContext ac = ActionContext.getContext();
			Map<String,Object> sess = ac.getSession();			
			YxyUserInfo user=(YxyUserInfo) sess.get("yxyuser");
			loginid=user.getLogin_id();//用户账号
			domain=user.getDomain();//用户所属域名
			smtplistall=yxysendsmtpinfoservice.findAllList();			
		} catch (Exception e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
	        String str = sw.toString();
	        LogClass.errolog(str,loginid+"@"+domain);
		}
		return "smtplist";
	}
	
	/**添加/编辑SMTP信息*/
	public String addSmtpInfo(){
		try {
			//获取用户session
			ActionContext ac = ActionContext.getContext();
			Map<String,Object> sess = ac.getSession();			
			YxyUserInfo user=(YxyUserInfo) sess.get("yxyuser");
			loginid=user.getLogin_id();//用户账号
			domain=user.getDomain();//用户所属域名
			//当前时间
			SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String nowDate=df.format(new Date());
			
			yxysendsmtpinfo.setSmtp_user_domain(domain);
			yxysendsmtpinfo.setSmtp_user_loginid(loginid);
			yxysendsmtpinfo.setSmtp_service_creatdate(nowDate);
			yxysendsmtpinfo.setSmtp_service_isuse(0);
			yxysendsmtpinfo.setSmtp_service_sendNum(0);
			yxysendsmtpinfo.setSmtp_service_userdate(nowDate);
			//是营销还是自配
			Map domaingraph=(Map) sess.get("domaingraph");
			int isself=Integer.parseInt(domaingraph.get("yxy_is_self").toString());
			yxysendsmtpinfo.setSmtp_service_isyxy(isself);
			//密码加密
			DES_Encrypt desEncrypt = new DES_Encrypt();
        	desEncrypt.setKey("06");
			desEncrypt.setEncString(yxysendsmtpinfo.getSmtp_service_password());
			yxysendsmtpinfo.setSmtp_service_password(desEncrypt.getStrMi());
			
			yxysendsmtpinfoservice.addSmtpinfo(yxysendsmtpinfo);
			result="1";
		} catch (Exception e) {
			result="0";
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
	        String str = sw.toString();
	        LogClass.errolog(str,loginid+"@"+domain);
		}
		return SUCCESS;
	}
	
	/**SMTP账号是否存在*/
	public String findBySmtp(){
		try {
			//获取用户session
			ActionContext ac = ActionContext.getContext();
			Map<String,Object> sess = ac.getSession();			
			YxyUserInfo user=(YxyUserInfo) sess.get("yxyuser");
			loginid=user.getLogin_id();//用户账号
			domain=user.getDomain();//用户所属域名
			smtplist=yxysendsmtpinfoservice.findByAccount(loginid, domain, account);
			if(smtplist.size()>0){
				result="0";
			}else{
				result="1";
			}
		} catch (Exception e) {
			result="0";
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
	        String str = sw.toString();
	        LogClass.errolog(str,loginid+"@"+domain);
		}
		return SUCCESS;
	}
	
	/**SMTP服务器删除*/
	public String delSmtp(){
		try {
			//获取用户session
			ActionContext ac = ActionContext.getContext();
			Map<String,Object> sess = ac.getSession();			
			YxyUserInfo user=(YxyUserInfo) sess.get("yxyuser");
			loginid=user.getLogin_id();//用户账号
			domain=user.getDomain();//用户所属域名
			yxysendsmtpinfoservice.delSmtp(smtpid);
			result="1";
		} catch (Exception e) {
			result="0";
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
	        String str = sw.toString();
	        LogClass.errolog(str,loginid+"@"+domain);
		}
		return SUCCESS;
	}
	
	/**模糊查询SMTP*/
	public String findSmtpLike(){
		try {
			//获取用户session
			ActionContext ac = ActionContext.getContext();
			Map<String,Object> sess = ac.getSession();			
			YxyUserInfo user=(YxyUserInfo) sess.get("yxyuser");
			loginid=user.getLogin_id();//用户账号
			domain=user.getDomain();//用户所属域名
			smtplist=yxysendsmtpinfoservice.findByLike(loginid, domain, account);
		} catch (Exception e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
	        String str = sw.toString();
	        LogClass.errolog(str,loginid+"@"+domain);
		}
		return "list";
	}
	
	/**SMTP服务器验证*/
	public String connectionService() {
		try {
			//获取用户session
			ActionContext ac = ActionContext.getContext();
			Map<String,Object> sess = ac.getSession();			
			YxyUserInfo user=(YxyUserInfo) sess.get("yxyuser");
			loginid=user.getLogin_id();//用户账号
			domain=user.getDomain();//用户所属域名
			String smtphost=yxysendsmtpinfo.getSmtp_service_number();
			String smtpname=yxysendsmtpinfo.getSmtp_service_account();
			//解密密码
			DES_Encrypt desEncrypt = new DES_Encrypt();
	    	desEncrypt.setKey("06");
			desEncrypt.setDesString(yxysendsmtpinfo.getSmtp_service_password());
			String smtppass=desEncrypt.getStrM();//smtp密码需要解密
			
			String isport=yxysendsmtpinfo.getSmtp_is_ssl()+"";
			String port=yxysendsmtpinfo.getSmtp_service_port();
			
			Properties props = new Properties();
			props.put("mail.smtp.host", smtphost);
			props.put("mail.smtp.auth", "true");// 设定发送邮件时需要进行身份验证
			
			String ishost=smtphost.split("\\.")[1];
			//判断是否需要ssl验证
			if(isport!=null&&!isport.equals("0")&&isport.equals("1")){
				if("gmail".equals(ishost)){
					props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
					props.put("mail.smtp.socketFactory.fallback", "false");
					props.put("mail.smtp.port", port);
					props.put("mail.smtp.socketFactory.port", port);
				}else{
					props.put("mail.smtp.starttls.enable","true");
					props.put("mail.smtp.port", port);
				}
			}
			javax.mail.Session mailSession = javax.mail.Session.getInstance(props);
			mailSession.setDebug(false); 
			Transport transport = mailSession.getTransport("smtp");
			transport.connect(smtphost, smtpname, smtppass);
			result="1";
			return SUCCESS;
		} catch (Exception e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
	        String str = sw.toString();
	        LogClass.errolog(str,loginid+"@"+domain);
	        if(str.indexOf("wrong password")!=-1||str.indexOf("AuthenticationFailedException")!=-1){//密码错
	        	result="2";
	        	return SUCCESS;
	        }else if(str.indexOf("login failed")!=-1){//服务器登录失败
	        	result="3";
	        	return SUCCESS;
	        }else if(str.indexOf("Could not connect to SMTP host")!=-1){//不能链接服务器
	        	result="4";
	        	return SUCCESS;
	        }else{
	        	result="0";
	        	return SUCCESS;
	        }
		}
	}
	
	private String loginid="erro";
	private String domain="erro";
	private String result;//返回结果
	private String account;//SMTP帐号
	private int smtpid;//SMTPID
	private List<YxySendSmtpInfo> smtplist=new ArrayList<YxySendSmtpInfo>();
	private List<YxySmtpList> smtplistall=new ArrayList<YxySmtpList>();
	private YxySendSmtpInfo yxysendsmtpinfo;//smtp pojo
	private YxySendSmtpInfoService yxysendsmtpinfoservice;//SMTP Service
	public YxySendSmtpInfoService getYxysendsmtpinfoservice() {
		return yxysendsmtpinfoservice;
	}
	public void setYxysendsmtpinfoservice(
			YxySendSmtpInfoService yxysendsmtpinfoservice) {
		this.yxysendsmtpinfoservice = yxysendsmtpinfoservice;
	}
	public List<YxySendSmtpInfo> getSmtplist() {
		return smtplist;
	}
	public void setSmtplist(List<YxySendSmtpInfo> smtplist) {
		this.smtplist = smtplist;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public List<YxySmtpList> getSmtplistall() {
		return smtplistall;
	}
	public void setSmtplistall(List<YxySmtpList> smtplistall) {
		this.smtplistall = smtplistall;
	}
	public YxySendSmtpInfo getYxysendsmtpinfo() {
		return yxysendsmtpinfo;
	}
	public void setYxysendsmtpinfo(YxySendSmtpInfo yxysendsmtpinfo) {
		this.yxysendsmtpinfo = yxysendsmtpinfo;
	}
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public int getSmtpid() {
		return smtpid;
	}
	public void setSmtpid(int smtpid) {
		this.smtpid = smtpid;
	}
}