1 依赖
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
2 util
package com.cyq.util;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.util.Map;
import java.util.Properties;
@Service
public class MailSender implements InitializingBean {
private static final Logger logger = LoggerFactory.getLogger(MailSender.class);
private JavaMailSenderImpl mailSender;
@Autowired
private VelocityEngine velocityEngine; //模板引擎
public boolean sendWithHTMLTemplate(String to, String subject,
String template, Map<String, Object> model) {
try {
//昵称
String nick = MimeUtility.encodeText("麻辣拌不要辣。");
//发送者邮箱地址course@cyq.com
InternetAddress from = new InternetAddress(nick + "<course@cyq.com>");
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
String result = VelocityEngineUtils
.mergeTemplateIntoString(velocityEngine, template, "UTF-8", model);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setSubject(subject);
//html:true
mimeMessageHelper.setText(result, true);
mailSender.send(mimeMessage);
return true;
} catch (Exception e) {
logger.error("发送邮件失败" + e.getMessage());
return false;
}
}
@Override
public void afterPropertiesSet() throws Exception {
mailSender = new JavaMailSenderImpl();
mailSender.setUsername("course@cyq.com");
mailSender.setPassword("123456");
//qq邮箱
mailSender.setHost("smtp.exmail.qq.com");
mailSender.setPort(465);
mailSender.setProtocol("smtps");
mailSender.setDefaultEncoding("utf8");
Properties javaMailProperties = new Properties();
javaMailProperties.put("mail.smtp.ssl.enable", true);
mailSender.setJavaMailProperties(javaMailProperties);
}
}
3 LoginExceptionHandler
package com.cyq.async.handler;
import com.cyq.async.EventHandler;
import com.cyq.async.EventModel;
import com.cyq.async.EventType;
import com.cyq.util.MailSender;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class LoginExceptionHandler implements EventHandler {
@Autowired
MailSender mailSender;
@Override
public void doHandle(EventModel model) {
// 判断发现这个用户登陆异常
Map<String, Object> map = new HashMap<String, Object>();
map.put("username", model.getExt("username"));
//login_exception.html是自定义错误信息页面
mailSender.sendWithHTMLTemplate(model.getExt("email"), "登陆IP异常", "mails/login_exception.html", map);
}
@Override
public List<EventType> getSupportEventTypes() {
return Arrays.asList(EventType.LOGIN);
}
}
##4 login_exception.html
你好$username,你的登陆有问题!