燕山石化溯源三维电子沙盘-【后端】-服务
13693261870
2024-03-21 c2caa2b7bbdfb9ddc4a652ade9ae96e96ea92929
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
package com.yssh.utils;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
 
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
 
/**
 * 邮件工具类
 *
 * @author www
 * @date 2024-03-21
 */
public class EmailUtils {
    @Value("${email.userName}")
    private String userName;
 
    @Value("${email.password}")
    private String password;
 
    @Value("${email.smtpHost}")
    private String smtpHost;
 
    @Value("${email.smtpPort}")
    private String smtpPort;
 
    @Value("${email.smtpAuth}")
    private String smtpAuth;
 
    @Value("${email.smtpTls}")
    private String smtpTls;
 
    @Value("${email.from}")
    private String from;
 
    @Value("${email.to}")
    private String to;
 
    @Value("${email.cc}")
    private String cc;
 
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
    public Session createSession() {
        // 创建一个配置文件,并保存
        Properties props = new Properties();
 
        // SMTP服务器连接信息:126—smtp.126.com,163—smtp.163.com,qq-qqsmtp.qq.com"
        props.put("mail.smtp.host", smtpHost); // SMTP主机名
        props.put("mail.smtp.port", smtpPort); // 主机端口号:126—25,163—645
        props.put("mail.smtp.auth", smtpAuth); // 是否需要用户认证
        props.put("mail.smtp.starttls.enale", smtpTls); // 启用TlS加密
 
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        });
 
        // 控制台打印调试信息
        session.setDebug(true);
 
        return session;
    }
 
    public void send(String title, String text) {
        try {
            // 创建Session会话
            Session session = createSession();
 
            // 创建邮件对象
            MimeMessage message = new MimeMessage(session);
            message.setSubject(title);
            message.setText(text);
            message.setFrom(new InternetAddress(from));
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
            //message.setRecipients(Message.RecipientType.CC, new InternetAddress[] {new  InternetAddress("抄送人邮箱")});
            if (!StringUtils.isEmpty(cc)) {
                String[] strs = cc.split(",");
                InternetAddress[] ias = new InternetAddress[strs.length];
                for (int i = 0, c = strs.length; i < c; i++) {
                    ias[i] = new InternetAddress(strs[i]);
                }
                message.setRecipients(Message.RecipientType.CC, ias);
            }
 
            // 发送
            Transport.send(message);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
}