jiangping
2025-06-17 64fa2c33cd645e86d4e2a8c34c7881ea4aa678cf
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
package com.doumee.service.common;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
 
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;
import java.util.Map;
 
@Service
@Slf4j
public class EmailService {
    @Autowired
    private JavaMailSender javaMailSender;//注入JavaMailSender
    @Value("${spring.mail.username}")
    private String fromEmail;
    public boolean sendEmailWithLocalFiles(String toEmail, String title, String content, List<Map<String,Object>> fileList) {
        try {
                AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
                context.refresh();
                MimeMessage message = javaMailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message, true);
                helper.setTo(toEmail);
                helper.setFrom(fromEmail);
                helper.setSubject(title);
                helper.setText(content);
                if(fileList!=null){
                    for (Map<String,Object> f : fileList){
                        // 设置附件
                        helper.addAttachment((String) f.get("name"),new FileSystemResource((File) f.get("file")));
                    }
                }
            javaMailSender.send(message);
                    System.out.println("邮件发送成功!");
                    return true;
            } catch (Exception e) {
            e.printStackTrace();
                    return false;
                }
            }
    public boolean sendEmailWithImages(String toEmail, String title, Map<String,String> contentForm, List<String> imgList) {
        log.error("邮件开始发送============"+toEmail);
        try {
                AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
                context.refresh();
                MimeMessage message = javaMailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message, true);
                helper.setTo(toEmail);
                helper.setFrom(fromEmail);
                helper.setSubject(title);
                String content = "<html><body>";
                if(contentForm!=null){
                    for (Map.Entry<String, String> f : contentForm.entrySet()) {
                        // 设置附件
                        content += "<div style='display:block;'>"+f.getKey()+":"+f.getValue()+"</p>";
                    }
                }
                if(imgList!=null){
                    content += "<div style='display:block;'> ";
                    for (String f : imgList){
                        // 设置附件
                        content += "<a href='"+f+"' target='blank'><img style='width:200px;margin:5px' src='"+f+"'/></a>";
                    }
                }
                content += "</div></body><html>";
                helper.setText(content,true);
                log.error("邮件内容============"+content);
                javaMailSender.send(message);
                log.error("邮件发生成功============");
                return true;
            } catch (Exception e) {
            e.printStackTrace();
            log.error("邮件发生成功============"+e.getMessage());
                    return false;
                }
            }
    }