JavaMail发送邮件



package com.fix.ciss.quartz.job;

import java.sql.SQLException;
import java.util.HashMap;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.fix.ciss.mail.MailSender;
import com.fix.ciss.mail.MailSenderInfo;
import com.founder.fix.apputil.log.DebugLog;
import com.founder.fix.apputil.log.LogFactory;
import com.founder.fix.dbcore.DBGetResult;
import com.founder.fix.dbcore.DBGetResultHandle;

public class TaskOfMailJob implements Job {

	/**
	 * 用改执行发送邮件功能 修改日期:2012/05/10
	 * 
	 * @author pengpan
	 */
	private static final DebugLog log = LogFactory.getDebugLog(TaskOfMailJob.class);

	@Override
	public synchronized void execute(JobExecutionContext context)
			throws JobExecutionException {
		JobDataMap param = context.getMergedJobDataMap();
		DBGetResult result = null;
		if (param != null) {
			HashMap map = (HashMap) param.get("param");
			String mail_subject = (String) map.get("mail_subject");
			String tesk_content = (String) map.get("tesk_content");
			String receive_addr = (String) map.get("receive_addr");
			Long task_id = (Long) map.get("task_id");
			MailSenderInfo mailSendInfo = (MailSenderInfo)map.get("mailSendInfo");
			MailSenderInfo mailInfo = new MailSenderInfo();
			mailInfo.setMailServerHost(mailSendInfo.getMailServerHost()); // 发送邮件的服务器IP
			mailInfo.setMailServerPost(mailSendInfo.getMailServerPost());
			mailInfo.setProtocol(mailSendInfo.getProtocol());
			mailInfo.setValidate(true);
			mailInfo.setUserName(mailSendInfo.getUserName()); // 发送者用户名
			mailInfo.setPassword(mailSendInfo.getPassword()); // 发送邮箱密码
			mailInfo.setToAddress(receive_addr); // 接受者地址
			mailInfo.setFromAddress(mailSendInfo.getFromAddress()); // 邮件发送者的地址
			mailInfo.setSubject(mail_subject);
			mailInfo.setContent(tesk_content);
			log.info("开始发送邮件");
			boolean sendFlag = MailSender.sendHtmlMail(mailInfo);
			try {
				result = DBGetResultHandle.createDBGetResult();
				if (sendFlag) {
					log.debug("TASK_ID:"+task_id+"发送成功");
					result.execSQLCmd("UPDATE T_QUARTZ_TASK SET TASK_STATUE = '2' WHERE TASK_ID="+task_id);
				} else {
					log.debug("TASK_ID:"+task_id+"发送失败");
					result.execSQLCmd("UPDATE T_QUARTZ_TASK SET TASK_STATUE = '3' WHERE TASK_ID="+task_id);
				}
			} catch (Exception e) {
				log.debug(e.getMessage());
				log.debug("TASK_ID:"+task_id+"发送失败");
				try {
					result.execSQLCmd("UPDATE T_QUARTZ_TASK SET TASK_STATUE = '3' WHERE TASK_ID="+task_id);
				} catch (SQLException e1) {
				    log.error(e.getMessage());
					e1.printStackTrace();
				}
				e.printStackTrace();
			}finally{
				try {
					DBGetResultHandle.closeDBGetResult(result);
				} catch (SQLException e) {
					log.error(e.getMessage());
					e.printStackTrace();
				}
			}

		}
	}
}

package com.fix.ciss.mail;

import java.util.Date;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.fix.ciss.quartz.job.TaskOfMailJob;
import com.founder.fix.apputil.log.DebugLog;
import com.founder.fix.apputil.log.LogFactory;

/**
 * 简单邮件(不带附件的邮件)
 * @author pengpan
 * 
 */
public class MailSender {

	/**
	 * 以HTML格式发送邮件 支持html元素
	 * @param mailInfo待发送的邮件信息
	 * @return
	 */
	private static final DebugLog log = LogFactory.getDebugLog(MailSender.class);
	public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
		MyAuthenticator authenticator = null;
		Properties pro = mailInfo.getProperties();
		if (mailInfo.isValidate()) {
			authenticator = new MyAuthenticator(mailInfo.getUserName(),
					mailInfo.getPassword()); // 如果需要身份认证,则创建一个密码验证器
		}
		Session sendMailSession = Session
				.getDefaultInstance(pro, authenticator);
		try {
			if(!checkMailPattern(mailInfo.getToAddress())){
				 log.debug("接收地址不正确");
			     return false;
			}
			Address from = new InternetAddress(mailInfo.getFromAddress());
			Message mailMessage = new MimeMessage(sendMailSession);
			mailMessage.setFrom(from);
			Address to = new InternetAddress(mailInfo.getToAddress());
			mailMessage.setRecipient(Message.RecipientType.TO, to);

			mailMessage.setSubject(mailInfo.getSubject());
			mailMessage.setSentDate(new Date());

			// MiniMultipart类是一个容器类,包含MineBodyPart类型的对象
			Multipart mainPart = new MimeMultipart();
			BodyPart html = new MimeBodyPart();
			html.setContent(mailInfo.getContent(), "text/html;charset=utf-8");
			mainPart.addBodyPart(html);
			mailMessage.setContent(mainPart); // 将MiniMultipart对象设置为邮件内容
			Transport.send(mailMessage);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

	}
	
	private static boolean checkMailPattern(String mailAdress){
		 Pattern p = Pattern.compile("\\w{1,}@\\w{1,}.[a-zA-Z]{2,}");
		 Matcher m = p.matcher(mailAdress);
		 return m.matches();
	}
}




package com.fix.ciss.mail;

import java.io.Serializable;
import java.util.Properties;

public class MailSenderInfo implements Serializable{
	private String mailServerHost; // 发送邮件的服务器
	private int mailServerPost; // 发送邮件的服务器端口
	private String protocol; // 发送邮件的模式(协议)

	public String getProtocol() {
		return protocol;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	private String fromAddress; // 邮件发送者地址
	private String toAddress; // 邮件接受者地址
	private String userName; // 登录邮件发送服务器的用户名
	private String password; // 登录邮件发送服务器的密码
	private boolean validate = false; // 是否需要身份验证
	private String subject; // 邮件主题
	private String content; // 邮件内容
	private String[] attachFileNames; // 邮件附件的文件名

	public Properties getProperties() {
		Properties p = new Properties();
		p.put("mail.smtp.host", this.mailServerHost);
		p.put("mail.smtp.port", this.mailServerPost);
		p.put("mail.smtp.auth", validate ? "true" : "false");
		p.put("mail.transport.protocol", this.protocol);
		return p;
	}

	public String getMailServerHost() {
		return mailServerHost;
	}

	public void setMailServerHost(String mailServerHost) {
		this.mailServerHost = mailServerHost;
	}

	public int getMailServerPost() {
		return mailServerPost;
	}

	public void setMailServerPost(int mailServerPost) {
		this.mailServerPost = mailServerPost;
	}

	public String getFromAddress() {
		return fromAddress;
	}

	public void setFromAddress(String fromAddress) {
		this.fromAddress = fromAddress;
	}

	public String getToAddress() {
		return toAddress;
	}

	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isValidate() {
		return validate;
	}

	public void setValidate(boolean validate) {
		this.validate = validate;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String[] getAttachFileNames() {
		return attachFileNames;
	}

	public void setAttachFileNames(String[] attachFileNames) {
		this.attachFileNames = attachFileNames;
	}
}

package com.fix.ciss.mail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class MyAuthenticator extends Authenticator {
	String userName = null;
	String password = null;

	public MyAuthenticator() {

	}

	public MyAuthenticator(String userName, String password) {
		this.userName = userName;
		this.password = password;
	}

	protected PasswordAuthentication getPasswordAuthentication() {

		return new PasswordAuthentication(userName, password);
	}
}



你可能感兴趣的:(exception,String,quartz,properties,HashMap,javamail)