wang-hao-jie
2021-11-09 6f81429892b6e751056ba250a19a6ecf746fa729
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package cn.exrick.xboot.core.common.utils;
 
import cn.exrick.xboot.core.common.constant.SettingConstant;
import cn.exrick.xboot.core.common.exception.XbootException;
import cn.exrick.xboot.core.entity.Setting;
import cn.exrick.xboot.core.service.SettingService;
import cn.exrick.xboot.core.vo.EmailSetting;
import cn.hutool.core.util.StrUtil;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
 
import javax.mail.internet.MimeMessage;
import java.util.Map;
import java.util.Properties;
 
/**
 * @author Exrickx
 */
@Component
@Slf4j
public class EmailUtil {
 
    @Autowired
    private SettingService settingService;
 
    @Autowired
    private TemplateEngine templateEngine;
 
    public EmailSetting getEmailSetting() {
 
        Setting setting = settingService.get(SettingConstant.EMAIL_SETTING);
        if (StrUtil.isBlank(setting.getValue())) {
            throw new XbootException("您还未配置邮件发送相关配置");
        }
        return new Gson().fromJson(setting.getValue(), EmailSetting.class);
    }
 
    /**
     * 异步发送邮件
     * @param sendTo       接收者邮箱
     * @param title        邮件标题
     * @param templateName 邮件模板
     * @param variables            模板替换变量Map
     */
    @Async
    public void sendTemplateEmailByMap(String sendTo, String title, String templateName, Map<String, Object> variables) {
 
        sendTemplateEmail(sendTo, title, templateName, variables, null);
    }
 
    /**
     * 异步发送邮件
     * @param sendTo       接收者邮箱
     * @param title        邮件标题
     * @param templateName 邮件模板
     * @param o            模板替换对象
     */
    @Async
    public void sendTemplateEmail(String sendTo, String title, String templateName, Object o) {
 
        sendTemplateEmail(sendTo, title, templateName, null, o);
    }
 
    private void sendTemplateEmail(String sendTo, String title, String templateName, Map<String, Object> variables, Object o) {
 
        EmailSetting es = getEmailSetting();
 
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
 
        // 设定邮箱服务器配置
        senderImpl.setHost(es.getHost());
        senderImpl.setUsername(es.getUsername());
        senderImpl.setPassword(es.getPassword());
        Properties prop = new Properties();
 
        // 服务器进行认证
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.timeout", "20000");
        // 邮箱发送服务器端口,这里设置为465端口
        prop.setProperty("mail.smtp.port", "465");
        prop.setProperty("mail.smtp.socketFactory.port", "465");
        prop.put("mail.smtp.ssl.enable", "true");
        senderImpl.setJavaMailProperties(prop);
 
        // 发送html邮件
        MimeMessage mailMessage = senderImpl.createMimeMessage();
        MimeMessageHelper messageHelper = null;
        // 设置邮件内容
        try {
            messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
 
            messageHelper.setTo(sendTo);
            messageHelper.setFrom(es.getUsername());
            messageHelper.setSubject(title);
            Context context = new Context();
            if (o != null) {
                context.setVariables(ObjectUtil.beanToMap(o));
            } else {
                context.setVariables(variables);
            }
            // 获取模板html代码
            String content = templateEngine.process(templateName, context);
            // true表示HTML格式的邮件
            messageHelper.setText(content, true);
            // 发送邮件
            senderImpl.send(mailMessage);
        } catch (Exception e) {
            log.error(e.toString());
            throw new XbootException("发送邮件失败,请检查邮件配置");
        }
    }
}