Helius
2021-06-21 5812839f3e23bb09e1a9b100b63273a646155709
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.xzx.log.service;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ReflectUtil;
import com.xzx.log.constants.Constants;
import com.xzx.log.entity.IntefaceAdminLog;
import com.xzx.log.util.SpringUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
 
import javax.mail.internet.MimeMessage;
import java.util.List;
 
@Service
@Slf4j
public class MailService {
 
    @Autowired
    private JavaMailSender sender;
 
    @Autowired
    private Configuration freemarkerConfig;
 
    @Value("${spring.mail.username}")
    private String from;
 
    @Value("${spring.mail.to}")
    private String to;
 
    public <T> void sendEmail(String serviceName, T t) {
        MimeMessage message = sender.createMimeMessage();
 
        MimeMessageHelper helper = new MimeMessageHelper(message);
 
        freemarkerConfig.setClassForTemplateLoading(this.getClass(), "/templates");
 
        Template template = null;
        try {
            template = freemarkerConfig.getTemplate("email-template.ftl");
            String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, t);
            helper.setFrom(from);
            helper.setTo(to.split(","));
            helper.setText(text, true);
            helper.setSubject("小棕熊" + serviceName + "错误");
            sender.send(message);
            log.info("发送邮件完成");
        } catch (Exception e) {
            log.error("邮件发送失败", e);
        }
    }
 
    @Async
    public <T> void sendEmail(List<T> list) {
        if (CollUtil.isNotEmpty(list) && SpringUtil.isProd()) {
            //只在生产环境发送邮件
            for (T t : list) {
                String logLevel = (String) ReflectUtil.getFieldValue(t, "logLevel");
                String implementType = (String) ReflectUtil.getFieldValue(t, "implementType");
                String serviceName = "";
                if (Constants.ADMIN_TYPE.equals(implementType)) {
                    serviceName = "后台系统";
                } else if (Constants.INTEFACE_TYPE.equals(implementType)) {
                    serviceName = "前端接口";
                }
                if (Constants.ERROR_LEVEL.equals(logLevel)) {
                    sendEmail(serviceName, t);
                }
            }
 
        }
 
    }
}