李宇
2021-01-12 fce41585f6c4e09c12c7e2a3150d4dfbf1f46bb0
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
package com.nanometer.smartlab.util;
 
import com.nanometer.smartlab.exception.BusinessException;
import com.nanometer.smartlab.exception.ExceptionEnumCode;
import org.apache.log4j.Logger;
 
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Properties;
 
public class EmailSend {
 
    private static Logger logger = Logger.getLogger(EmailSend.class);
 
    public static void send(String to, String applyCode) {
 
        Transport transport = null;
        try {
 
            EmailConfigService cs = SpringUtil.getBean("emailConfigService", EmailConfigService.class);
            String smtp = cs.getSmtpHost();
            String user = cs.getSmtpUser();
            String password = cs.getSmtpPwd();
            String from = cs.getSmtpFrom();
            String subject = cs.getSmtpSubject();
            String content = MessageFormat.format(cs.getSmtpContent(), applyCode);
 
            Properties props = new Properties();
            props.put("mail.smtp.host", smtp);
            props.put("mail.smtp.auth", "true");
            Session ssn = Session.getInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user, password);
                }
            });
            // 由邮件会话新建一个消息对象
            MimeMessage message = new MimeMessage(ssn);
            // 发件人的邮件地址
            InternetAddress fromAddress = new InternetAddress(from);
            // 设置发件人
            message.setFrom(fromAddress);
            // 收件人的邮件地址
            InternetAddress toAddress = new InternetAddress(to);
            // 设置收件人
            message.addRecipient(Message.RecipientType.TO, toAddress);
            // 设置标题
            message.setSubject(subject);
            // 设置内容
            message.setText(content);
            // 设置发信时间
            message.setSentDate(new Date());
 
            transport = ssn.getTransport("smtp");
            transport.connect(smtp, user, password);
            transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.MAIL_SEND_FAIL, "邮件发送失败。", ex);
        } finally {
            if (transport != null) {
                try {
                    transport.close();
                } catch (Exception ex) {
 
                }
            }
        }
    }
}