kongzy
2024-07-01 47a751cb301d05276ae5d75145d57b2d090fe4e1
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import com.sun.mail.util.MailSSLSocketFactory;
 
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) {
 
                }
            }
        }
    }
 
 
    public static void sendArrivalEmail(List<String> revicerList, String msg, String title){
        EmailConfigService cs = SpringUtil.getBean("emailConfigService", EmailConfigService.class);
        String sender = cs.getZkySmtpUser();/*发送人*/
        String password = cs.getZkySmtpPwd();
 
        String ZkySmtp = "smtp.cstnet.cn"; /*中科院邮件服务器*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Properties prop = new Properties();
        prop.setProperty("mail.host", ZkySmtp);
        prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议
        prop.setProperty("mail.smtp.auth", "true"); // 需要验证用户名密码
        prop.setProperty("mail.smtp.port", "25");
 
        MailSSLSocketFactory mailSSLSocketFactory = null;
        try{
            mailSSLSocketFactory = new MailSSLSocketFactory();
            mailSSLSocketFactory.setTrustAllHosts(true);
        }catch (Exception e){
            logger.error("邮件开启 MailSSLSocketFactory 失败:"+e.getMessage());
        }
 
        if (mailSSLSocketFactory != null) {
 
            prop.put("mail.smtp.ssl.enable", false);
            prop.put("mail.smtp.ssl.socketFactory", mailSSLSocketFactory);
            //创建定义整个应用程序所需的环境信息的 Session 对象
            Session session = Session.getDefaultInstance(prop, new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    //发件人邮件用户名、授权码
                    return new PasswordAuthentication(sender, password);
                }
            });
 
            for (String revicer : revicerList) {
                try {
                    //2、通过session得到transport对象
                    Transport ts = session.getTransport();
                    //3、使用邮箱的用户名和授权码连上邮件服务器
                    ts.connect(sender, password);
                    try {
                        //4、创建邮件
                        //创建邮件对象
                        MimeMessage message = new MimeMessage(session);
                        //指明邮件的发件人
                        message.setFrom(new InternetAddress(sender));
                        //指明邮件的收件人
                        message.setRecipient(Message.RecipientType.TO, new InternetAddress(revicer));
                        //邮件的标题
                        message.setSubject(sdf.format(new Date())+title);
                        //邮件的文本内容
                        message.setContent(msg, "text/html;charset=UTF-8");
                        //5、发送邮件
                        ts.sendMessage(message, message.getAllRecipients());
                    } catch (Exception e) {
                        logger.error("邮件发送" + "失败:" + e.getMessage());
                    }
                    ts.close();
                } catch (Exception e) {
                    logger.error("邮件发送失败:" + e.getMessage());
                }
            }
        }
        logger.error("邮件发送完成" + "时间:" + sdf.format(new Date()));
    }
}