基于SpringBoot如何实现发送带附件的邮件

  介绍

这篇文章给大家分享的是有关基于SpringBoot如何实现发送带附件的邮件的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

& lt; !——发送电子邮件依赖——比;   ,,,& lt; dependency>   ,,,,,& lt; groupId> org.springframework.boot   ,,,,,& lt; artifactId> spring-boot-starter-mail   ,,,& lt;/dependency> 服务

 package  com.ykmimi.job.service;
  import  org.springframework.stereotype.Service;
  import  java.util.Map;
  @ service
  public  interface  MailService  {
  ,/* *
  ,,*,TODO 发送带附件的邮件,,,需要进行重载方法
  ,,*/Map<才能,字符串,Object>, sendAttachmentsMail (String ,, String 主题,,String 内容,String  filePath);
  }

服务实现

package  com.ykmimi.job.service.impl;      import  com.ykmimi.job.service.MailService;   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  org.springframework.util.ResourceUtils;      import  javax.annotation.Resource;   import  javax.mail.MessagingException;   import  javax.mail.internet.MimeMessage;   import  java.io.File;   import  java.io.FileNotFoundException;   import  java.util.HashMap;   import  java.util.Map;      @ service   public  class  MailServiceImpl  implements  MailService  {      ,@   private 才能;JavaMailSender  mailSender;//发才能送者   @ value才能(“$ {mail.fromMail.addr}“)   private 才能;String 从;//TODO 才能设置发送邮件重载方式         @Override才能   public 才能;Map<字符串,Object>, sendAttachmentsMail (String ,, String 主题,,String 内容,String  filePath), {      ,,,System.out.println (“sendAttachmentsMail"拷贝);   ,,,System.out.println (filePath);   ,,,MimeMessage  message =, mailSender.createMimeMessage ();   ,,,Map<字符串,Object>, map =, new  HashMap<在();      ,,,try  {   ,,,,,MimeMessageHelper  helper =, new  MimeMessageHelper(消息,,真的);   ,,,,,helper.setFrom(从);   ,,,,,helper.setTo ();   ,,,,,helper.setSubject(主题);   ,,,,,helper.setText(内容,,真的);//,,,,,,FileSystemResource  file =, new  FileSystemResource (new 文件(filePath));   ,,,,,//,发送附件   ,,,,,File  File =, new 文件(filePath);   ,,,,,file =, ResourceUtils.getFile (file.getAbsolutePath ());   ,,,,,String  fileName =, filePath.substring (filePath.lastIndexOf (File.separator));//,,,,,,String  fileName =, filePath.substring (filePath.lastIndexOf (“/?);//,,,,,,String  fileName =,“附件“;   ,,,,,System.out.println(文件名);   ,,,,,//添加多个附件可以使用多条,helper.addAttachment(文件名、文件);   ,,,,,//helper.addAttachment(文件名、文件);   ,,,,,helper.addAttachment(文件名,文件);   ,,,,,mailSender.send(消息);   ,,,,,map.put (“code",, 1);   ,,,,,map.put (“message",,“发送成功“);   ,,,,,return 地图;   ,,,},catch  (MessagingException  e), {   ,,,,,e.printStackTrace ();   ,,,,,map.put (“code" 0);   ,,,,,map.put (“message",“发送失败“);   ,,,,,return 地图;   ,,,},catch  (FileNotFoundException  e), {   ,,,,,e.printStackTrace ();   ,,,,,map.put (“code", 1);   ,,,,,map.put (“message",“没有文件“);   ,,,,,return 地图;   ,,,},{finally    ,,,}   ,,}   }

或将邮件内容及邮件地址等封装为EmailContent的bean实体

通过控制而接受。

下面是我的邮件主机配置,大家可以拿去用

# #上传文件限制大小   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

基于SpringBoot如何实现发送带附件的邮件