1
hao
2025-05-20 8e24c6fea30d9b179375ee2893710cdec2443b13
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
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.MimeMessage;
import java.io.File;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
 
/**
 * 邮件发送工具类
 * 说明:1.这里的邮件服务的接口JavaMailSender采取自动注入的方式,在配置文件中一定需要配置spring.mail.host、spring.
 * mail.port、 spring.mail.username、spring.mail.password,否则无法自动注入而程序报错;
 * 2.在MimeMessageHelper中设置发件人邮箱地址时,一定要与配置文件中的spring.mail.username的邮箱地址一致,否则无法发送。
 * 
 * @author sxw
 * @ClassName com.email.EmailUtils_2
 */
@Component
public class EmailUtils_2 {
 
    private static final Logger logger = LoggerFactory.getLogger(EmailUtils_2.class);
    @Autowired
    private Environment env;
    @Autowired
    private JavaMailSender mailSender;
    // @Autowired
    // private JavaMailSenderImpl mailSender;
    // 发件人邮箱
    private static String authName;
 
    // 初始化配置
    @PostConstruct
    public void initParam() {
        authName = env.getProperty("spring.mail.username");
    }
 
    /**
     * 发送邮件
     * 
     * @param subject
     *            主题
     * @param toUsers
     *            收件人
     * @param ccUsers
     *            抄送人
     * @param content
     *            邮件内容
     * @param attachfiles
     *            附件列表 List<Map<String, String>> key: name && file
     */
    public boolean sendEmail(String subject, String[] toUsers, String[] ccUsers, String content,
            List<Map<String, String>> attachfiles) {
        boolean flag = true;
        try {
            MimeMessage mailMessage = mailSender.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);
                }
            }
            mailSender.send(mailMessage);
        } catch (Exception e) {
            logger.error("发送邮件失败!", e);
            flag = false;
        }
        return flag;
    }
 
}