| | |
| | | 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.SimpleMailMessage; |
| | | import org.springframework.mail.javamail.JavaMailSender; |
| | | import org.springframework.mail.javamail.MimeMessageHelper; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | @Service |
| | | public class EmailService { |
| | | |
| | | @Autowired |
| | | private JavaMailSender javaMailSender;//注入JavaMailSender |
| | | private JavaMailSender javaMailSender; |
| | | |
| | | @Value("${spring.mail.username}") |
| | | private String fromEmail; |
| | | public boolean sendEmailWithLocalFiles(String toEmail, String title, String content, List<Map<String,Object>> fileList) { |
| | | |
| | | /** |
| | | * 发送纯文本邮件 |
| | | */ |
| | | public boolean sendText(String toEmail, String subject, String text) { |
| | | try { |
| | | AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| | | context.refresh(); |
| | | SimpleMailMessage message = new SimpleMailMessage(); |
| | | message.setTo(toEmail); |
| | | message.setFrom(fromEmail); |
| | | message.setSubject(subject); |
| | | message.setText(text); |
| | | javaMailSender.send(message); |
| | | log.info("纯文本邮件发送成功: to={}", toEmail); |
| | | return true; |
| | | } catch (Exception e) { |
| | | log.error("纯文本邮件发送失败: to={}, error={}", toEmail, e.getMessage()); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 发送HTML邮件 |
| | | */ |
| | | public boolean sendHtml(String toEmail, String subject, String html) { |
| | | try { |
| | | MimeMessage message = javaMailSender.createMimeMessage(); |
| | | MimeMessageHelper helper = new MimeMessageHelper(message, true); |
| | | helper.setTo(toEmail); |
| | | helper.setFrom(fromEmail); |
| | | helper.setSubject(title); |
| | | helper.setText(content); |
| | | helper.setSubject(subject); |
| | | helper.setText(html, true); |
| | | javaMailSender.send(message); |
| | | log.info("HTML邮件发送成功: to={}", toEmail); |
| | | return true; |
| | | } catch (Exception e) { |
| | | log.error("HTML邮件发送失败: to={}, error={}", toEmail, e.getMessage()); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 发送带附件邮件 |
| | | * @param fileList 附件列表,每个Map包含 name(文件名) 和 file(File对象) |
| | | */ |
| | | public boolean sendWithAttachment(String toEmail, String subject, String text, List<Map<String, Object>> fileList) { |
| | | try { |
| | | MimeMessage message = javaMailSender.createMimeMessage(); |
| | | MimeMessageHelper helper = new MimeMessageHelper(message, true); |
| | | helper.setTo(toEmail); |
| | | helper.setFrom(fromEmail); |
| | | helper.setSubject(subject); |
| | | helper.setText(text); |
| | | 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("邮件发送成功!"); |
| | | log.info("附件邮件发送成功: to={}", toEmail); |
| | | return true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("附件邮件发送失败: to={}, error={}", toEmail, e.getMessage()); |
| | | return false; |
| | | } |
| | | } |
| | | public boolean sendEmailWithImages(String toEmail, String title, Map<String,String> contentForm, List<String> imgList) { |
| | | |
| | | /** |
| | | * 发送带内嵌图片的HTML邮件 |
| | | * @param contentForm 表单内容键值对 |
| | | * @param imgList 图片URL列表 |
| | | */ |
| | | public boolean sendWithInlineImage(String toEmail, String subject, 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>"; |
| | | helper.setSubject(subject); |
| | | StringBuilder content = new StringBuilder("<html><body>"); |
| | | if(contentForm!=null){ |
| | | for (Map.Entry<String, String> f : contentForm.entrySet()) { |
| | | // 设置附件 |
| | | content += "<div style='display:block;'>"+f.getKey()+":"+f.getValue()+"</p>"; |
| | | for (Map.Entry<String, String> entry : contentForm.entrySet()) { |
| | | content.append("<div style='display:block;'>").append(entry.getKey()).append(":").append(entry.getValue()).append("</div>"); |
| | | } |
| | | } |
| | | 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.append("<div style='display:block;'>"); |
| | | for (String img : imgList) { |
| | | content.append("<a href='").append(img).append("' target='blank'><img style='width:200px;margin:5px' src='").append(img).append("'/></a>"); |
| | | } |
| | | content.append("</div>"); |
| | | } |
| | | content += "</div></body><html>"; |
| | | helper.setText(content,true); |
| | | content.append("</body></html>"); |
| | | helper.setText(content.toString(), true); |
| | | javaMailSender.send(message); |
| | | System.out.println("邮件发送成功!"); |
| | | log.info("图片邮件发送成功: to={}", toEmail); |
| | | return true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("图片邮件发送失败: to={}, error={}", toEmail, e.getMessage()); |
| | | return false; |
| | | } |
| | | } |