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

所属分类: 软件编程 / java 阅读数: 83
收藏 0 赞 0 分享

这篇文章主要介绍了基于SpringBoot实现发送带附件的邮件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    <!--发送email依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>

service

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

service实现

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 {

  @Resource
  private JavaMailSender mailSender;
  //发送者
  @Value("${mail.fromMail.addr}")
  private String from;

  //TODO 设置发送邮件重载方式


  @Override
  public Map<String, Object> sendAttachmentsMail(String to, String subject, String content, String filePath) {

    System.out.println("in sendAttachmentsMail");
    System.out.println(filePath);
    MimeMessage message = mailSender.createMimeMessage();
    Map<String, Object> map = new HashMap<>();

    try {
      MimeMessageHelper helper = new MimeMessageHelper(message, true);
      helper.setFrom(from);
      helper.setTo(to);
      helper.setSubject(subject);
      helper.setText(content, true);
//      FileSystemResource file = new FileSystemResource(new File(filePath));
      // 发送附件
      File file = new File(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(fileName);
      //添加多个附件可以使用多条 helper.addAttachment(fileName,file);
      //helper.addAttachment(fileName,file);
      helper.addAttachment(fileName, file);
      mailSender.send(message);
      map.put("code", 1);
      map.put("message", "发送成功");
      return map;
    } catch (MessagingException e) {
      e.printStackTrace();
      map.put("code",0);
      map.put("message","发送失败");
      return map;
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      map.put("code",-1);
      map.put("message","没有文件");
      return map;
    } finally {
    }
  }
}

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

通过Controll而接受.

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

##上传文件限制大小
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
##发送email设置
spring.application.name=spring-boot-mail
spring.mail.host=smtp.126.com
spring.mail.username=hostinbj@126.com
spring.mail.password=1314520jy
spring.mail.default-encoding=UTF-8
##邮件主机地址
mail.fromMail.addr=hostinbj@126.com
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
#邮件端口
spring.mail.properties.mail.smtp.socketFactory.port=465

spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java Set简介_动力节点Java学院整理

Set最大的特性就是不允许在其中存放的元素是重复的。接下来通过本文给大家分享java set常用方法和原理分析,需要的的朋友参考下吧
收藏 0 赞 0 分享

Java Timezone类常见问题_动力节点Java学院整理

这篇文章主要介绍了Java Timezone类常见问题的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

javaWeb项目部署到阿里云服务器步骤详解

本篇文章主要介绍了javaWeb项目部署到阿里云服务器步骤详解,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解使用zxing库生成QR-Code二维码

这篇文章主要介绍了详解使用zxing库生成QR-Code二维码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现对服务器的自动巡检邮件通知

这篇文章主要为大家详细介绍了java实现对服务器的自动巡检邮件通知,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java随机验证码生成实现实例代码

这篇文章主要介绍了java随机验证码生成实现实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

java读取txt文件代码片段

这篇文章主要为大家详细介绍了java读取txt文件的代码片段,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java连接mysql数据库的方法

这篇文章主要为大家详细介绍了java连接mysql数据库的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java 算法之快速排序实现代码

这篇文章主要介绍了java 算法之快速排序实现代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用

这篇文章主要介绍了详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多