package org.king.modules.message.handle.impl;

import org.king.common.util.SpringContextUtils;
import org.king.modules.message.handle.ISendMsgHandle;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

public class EmailSendMsgHandle implements ISendMsgHandle {

	private static String emailFrom;
	public static void setEmailFrom(String emailFrom) {
		EmailSendMsgHandle.emailFrom = emailFrom;
	}

	@Override
	public void sendmsg(String esReceiver, String esTitle, String esContent) {
		JavaMailSender mailSender = (JavaMailSender) SpringContextUtils.getBean("mailSender");
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = null;
		try {
			helper = new MimeMessageHelper(message, true);
			// 设置发送方邮箱地址
			helper.setFrom(emailFrom);
			helper.setTo(esReceiver);
			helper.setSubject(esTitle);
			helper.setText(esContent, true);
			mailSender.send(message);
		} catch (MessagingException e) {
			e.printStackTrace();
		}

	}
}
