| package com.doumee.service.business.third; | 
|   | 
| 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.activation.DataSource; | 
| import javax.mail.MessagingException; | 
| 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 sendEmail(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; | 
|                 } | 
|             } | 
|     } |