欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-12-11 09:12:20|栏目:JAVA代码|点击:

这篇文章主要介绍了基于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

上一篇:Json在Struts中的转换与传递方法

栏    目:JAVA代码

下一篇:浅谈MyBatis3 DynamicSql风格语法使用指南

本文标题:基于SpringBoot实现发送带附件的邮件

本文地址:http://www.codeinn.net/misctech/186504.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有