springboot拦截异常信息发送邮件提醒
-- private JavaMailSender sender; 可能会出现注入错误,请注意yam配置文件中格式是否一致;否则会找不到注入的bean
一 发送邮件
在Springboot中发送邮件非常简单。
pom.xml引入maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
在application.yml里设置发信人的账号、密码
spring: mail: host: smtp.qq.com username: 27255XXXX@qq.com password: njcvcbdkrofgbhie properties: mail: smtp: auth: true starttls: enable: true required: true
这个username就是未来发信时的邮箱地址,password是授权码。
这里以普通qq邮箱为例,注意password不是qq密码,而是授权码。
在qq邮箱-设置-账户,找到图片中的地方,开启IMAP/SMTP服务,开启后才能在别的客户端使用该qq邮箱发邮件,然后生成授权码,填写到application.yml的password位置。
然后就可以使用该邮箱作为发件人了。
看一下发邮件的具体代码,参考http://blog.csdn.net/clementad/article/details/51833416
package com.zhx.commonservice.common.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; /** * @Author: SimonHu * @Date: 2019/5/23 9:04 * @Description: */ @Service public class MailService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private JavaMailSender sender; @Value("${spring.mail.username}") private String from; /** * 发送纯文本的简单邮件 * @param to * @param subject * @param content */ public void sendSimpleMail(String to, String subject, String content){ SimpleMailMessage message = new SimpleMailMessage(); String[] toRecive = to.split(","); message.setFrom(from); message.setTo(toRecive); message.setSubject(subject); message.setText(content); try { sender.send(message); logger.info("简单邮件已经发送。"); } catch (Exception e) { logger.error("发送简单邮件时发生异常!", e); } } /** * 发送html格式的邮件 * @param to * @param subject * @param content */ public void sendHtmlMail(String to, String subject, String content){ MimeMessage message = sender.createMimeMessage(); try { String[] toRecive = to.split(","); //true表示需要创建一个multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(toRecive); helper.setSubject(subject); helper.setText(content, true); sender.send(message); logger.info("html邮件已经发送。"); } catch (MessagingException e) { logger.error("发送html邮件时发生异常!", e); } } /** * 发送带附件的邮件 * @param to * @param subject * @param content * @param filePath */ public void sendAttachmentsMail(String to, String subject, String content, String filePath){ MimeMessage message = sender.createMimeMessage(); try { String[] toRecive = to.split(","); //true表示需要创建一个multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(toRecive); helper.setSubject(subject); helper.setText(content, true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); sender.send(message); logger.info("带附件的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送带附件的邮件时发生异常!", e); } } /** * 发送嵌入静态资源(一般是图片)的邮件 * @param to * @param subject * @param content 邮件内容,需要包括一个静态资源的id,比如:<img src=\"cid:rscId01\" > * @param rscPath 静态资源路径和文件名 * @param rscId 静态资源id */ public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){ MimeMessage message = sender.createMimeMessage(); try { String[] toRecive = to.split(","); //true表示需要创建一个multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(toRecive); helper.setSubject(subject); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); sender.send(message); logger.info("嵌入静态资源的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送嵌入静态资源的邮件时发生异常!", e); } } }
然后就可以使用里面的方法发邮件了。
可以先写个简单的测试类,调用
mailService.sendSimpleMail("wuweifeng@XXX.com", "主题:简单邮件", "测试邮件内容");
填写个收信人的地址就OK了。然后就能收到邮件了。收信人可以有多个,通过SimpleMailMessage可以看到。
二 拦截全局异常并发邮件
定义一个全局拦截类
package com.tianyalei.testmail.global; import com.tianyalei.testmail.service.MailService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import javax.servlet.http.HttpServletRequest; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Enumeration; import static org.springframework.http.HttpStatus.NOT_EXTENDED; /** * Created by wuwf on 17/3/31. * 全局异常处理 */ @ControllerAdvice public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { private Logger logger = LoggerFactory.getLogger(getClass().getName()); @Autowired private MailService mailService; /** * 在controller里面内容执行之前,校验一些参数不匹配啊,Get post方法不对啊之类的 */ @Override protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) { System.out.println("错误"); return new ResponseEntity<>("出错了", NOT_EXTENDED); } @ExceptionHandler(value = Exception.class) @ResponseBody public String jsonHandler(HttpServletRequest request, Exception e) throws Exception { log(e, request); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); //发送邮件 mailService.sendSimpleMail("wuweifeng@XXXX.com", "异常", sw.toString()); return "发生异常"; } private void log(Exception ex, HttpServletRequest request) { logger.error("************************异常开始*******************************"); logger.error("请求地址:" + request.getRequestURL()); Enumeration enumeration = request.getParameterNames(); logger.error("请求参数"); while (enumeration.hasMoreElements()) { String name = enumeration.nextElement().toString(); logger.error(name + "---" + request.getParameter(name)); } StackTraceElement[] error = ex.getStackTrace(); for (StackTraceElement stackTraceElement : error) { logger.error(stackTraceElement.toString()); } logger.error("************************异常结束*******************************"); } }