forked from kidgrow-microservices-platform

dougang
2020-11-04 b5c197d06d35d56a9fbcb379e49ad07698d7dd90
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
package com.kidgrow.email.service.impl;
 
import com.kidgrow.email.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
 
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: <br>
 * @Project: <br>
 * @CreateDate: Created in 2020/2/17 11:34 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 */
@Service
public class MailServiceImpl implements MailService {
    @Autowired
    private JavaMailSender mailSender;
 
    @Value("${spring.mail.username}")
    private String from;
 
    /**
     * @param to
     * @param subject
     * @param content
     * @param files
     */
    @Override
    public void sendHtmlMail(List<String> to, String subject, String content, List<File> files) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);   //true表示需要创建一个multipart message
        helper.setFrom(from);
        helper.setTo(to.toArray(new String[to.size()]));
        helper.setSubject(subject);
        helper.setText(content, true);
        if (files != null) {
            for (File file : files) {
                try {
                    helper.addAttachment(file.getName(), file);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        mailSender.send(message);
        System.out.println("html邮件发送成功");
 
    }
}