1. 程式人生 > >SpringBoot攔截全域性異常併發送郵件給指定郵箱

SpringBoot攔截全域性異常併發送郵件給指定郵箱

主要是看一下Springboot中傳送郵件的方法,至於攔截Springboot全域性異常之前的文章中有。

一 傳送郵件

在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: 
[email protected]
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.tianyalei.testmail.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;

@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();
        message.setFrom(from);
        message.setTo(to);
        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 {
            //true表示需要建立一個multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            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 {
            //true表示需要建立一個multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            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 {
            //true表示需要建立一個multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            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("[email protected]", "主題:簡單郵件", "測試郵件內容");
填寫個收信人的地址就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("[email protected]", "異常", 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("************************異常結束*******************************");
    }
}
當有異常時會進入被@ExceptionHandler標註的方法中,在該方法裡做異常日誌的記錄和發郵件的邏輯。手動觸發個異常,看看結果


可以看到已經收到郵件了。