package com.doumee.service.common; 
 | 
  
 | 
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 
 | 
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) { 
 | 
        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); 
 | 
            javaMailSender.send(message); 
 | 
                    System.out.println("邮件发送成功!"); 
 | 
                    return true; 
 | 
            } catch (Exception e) { 
 | 
            e.printStackTrace(); 
 | 
                    return false; 
 | 
                } 
 | 
            } 
 | 
    } 
 |