4
hao
2025-04-16 c5fb1fbcbb2bf4d511773d348f9ef625855c61fc
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.email;
 
import com.sun.mail.util.MailSSLSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
 
/**
 * 邮件发送工具类
 * 说明:1.这里的邮件服务的接口JavaMailSender采取的是普通新建对象的方式实体化,即new一个JavaMailSenderImpl对象;
 * 2.需要在JavaMailSender中设置好所有相关的邮箱参数,否则无法发送。
 * 
 * @author sxw
 * @ClassName com.email.EmailUtils
 * @Description
 */
@Component
public class EmailUtils {
 
    private static final Logger logger = LoggerFactory.getLogger(EmailUtils.class);
 
    @Autowired
    private Environment env;
 
    private static String auth;
    private static String host;
    private static String protocol;
    private static int port;
    private static String authName;
    private static String password;
    private static boolean isSSL;
    private static String charset;
    private static String timeout;
 
    // 初始化配置
    @PostConstruct
    public void initParam() {
        auth = env.getProperty("mail.smtp.auth") != null ? env.getProperty("mail.smtp.auth") : "false";
        host = env.getProperty("mail.host");
        protocol = env.getProperty("mail.transport.protocol");
        port = env.getProperty("mail.smtp.port", Integer.class) != null
                ? env.getProperty("mail.smtp.port", Integer.class) : 25;
        authName = env.getProperty("mail.auth.name");
        password = env.getProperty("mail.auth.password");
        charset = env.getProperty("mail.send.charset");
        isSSL = env.getProperty("mail.is.ssl", Boolean.class) != null ? env.getProperty("mail.is.ssl", Boolean.class)
                : false;
        timeout = env.getProperty("mail.smtp.timeout") != null ? env.getProperty("mail.smtp.timeout") : "50000";
    }
 
    /**
     * 发送邮件
     * 
     * @param subject
     *            主题
     * @param toUsers
     *            收件人
     * @param ccUsers
     *            抄送人
     * @param content
     *            邮件内容
     * @param attachfiles
     *            附件列表 List<Map<String, String>> key: name && file
     */
    public static boolean sendEmail(String subject, String[] toUsers, String[] ccUsers, String content,
            List<Map<String, String>> attachfiles) {
        boolean flag = true;
        try {
            JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
            javaMailSender.setHost(host);
            javaMailSender.setUsername(authName);
            javaMailSender.setPassword(password);
            javaMailSender.setDefaultEncoding(charset);
            javaMailSender.setProtocol(protocol);
            javaMailSender.setPort(port);
 
            Properties properties = new Properties();
            properties.setProperty("mail.smtp.auth", auth);
            properties.setProperty("mail.smtp.timeout", timeout);
            if (isSSL) {
                MailSSLSocketFactory sf = null;
                try {
                    sf = new MailSSLSocketFactory();
                    sf.setTrustAllHosts(true);
                    properties.put("mail.smtp.ssl.enable", "true");
                    properties.put("mail.smtp.ssl.socketFactory", sf);
                } catch (GeneralSecurityException e) {
                    e.printStackTrace();
                }
            }
            javaMailSender.setJavaMailProperties(properties);
 
            MimeMessage mailMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);
            messageHelper.setTo(toUsers);
            if (ccUsers != null && ccUsers.length > 0) {
                messageHelper.setCc(ccUsers);
            }
            messageHelper.setFrom(authName);
            messageHelper.setSubject(subject);
            messageHelper.setText(content, true);
 
            if (attachfiles != null && attachfiles.size() > 0) {
                for (Map<String, String> attachfile : attachfiles) {
                    String attachfileName = attachfile.get("name");
                    File file = new File(attachfile.get("file"));
                    messageHelper.addAttachment(attachfileName, file);
                }
            }
            javaMailSender.send(mailMessage);
        } catch (Exception e) {
            logger.error("发送邮件失败!", e);
            flag = false;
        }
        return flag;
    }
 
    @Autowired
    static JavaMailSender jms;
 
    public static String send() {
        // 建立邮件消息
        SimpleMailMessage mainMessage = new SimpleMailMessage();
        // 发送者
        mainMessage.setFrom("fuyuanxiu@126.com");
        // 接收者
        mainMessage.setTo("yuanxiu.f@plee.com.cn");
        // 发送的标题
        mainMessage.setSubject("嗨喽");
        // 发送的内容
        mainMessage.setText("hello world");
        jms.send(mainMessage);
        return "1";
    }
 
    /**
     * 发送邮件
     * 
     * @param subject
     *            邮件主题
     * @param sendHtml
     *            邮件内容
     * @param toUser
     *            收件人 多个时参数形式 : "xxx@xxx.com,xxx@xxx.com,xxx@xxx.com"
     * @param ccUser
     *            抄送人 同上
     * @param bccUser
     *            密送人 同上
     * @param attachment
     *            附件
     */
    public void doSendHtmlEmail(String subject, String sendHtml, String toUser, String ccUser, String bccUser,
            File[] attachment) {
        try {
 
            MimeMessage mimeMessage = jms.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
 
            System.out.println("发送成功!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
 
        }
    }
 
    /**
     * 发送带附件的邮件
     * 
     * @param to
     *            接受者
     * @param subject
     *            主题
     * @param content
     *            内容
     * @param filePath
     *            文件路径
     */
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
        MimeMessage message = jms.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(env.getProperty("spring.mail.username"));
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content);
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
 
            helper.addAttachment(fileName, file);
 
            jms.send(message);
            System.out.println("带附件的邮件发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("发送带附件的邮件失败");
        }
    }
 
}