Springboot、EasyExcel 发送带附件的邮件

Springboot、EasyExcel 发送带附件的邮件,第1张

Springboot、EasyExcel 发送带附件的邮件 主要依赖

    com.alibaba
    easyexcel
    2.2.11



    org.springframework.boot
    spring-boot-starter-mail



    org.projectlombok
    lombok
    true

配置

此处的协议为 smtps,且配置了各种超时时间,若超时时间不起作用,请移步 springboot java mail 超时配置不生效。

spring:
  mail:
    protocol: smtps
    host: smtp.mxhichina.com
    port: 465
    username: xxx@xxxt.com
    password: xxxxxx
    properties:
      mail.smtps.connectiontimeout: 150000
      mail.smtps.timeout: 250000
      mail.smtps.writetimeout: 5000
      mail.smtp.auth: true
      mail.smtp.auth.starttls.required: true
      mail.smtp.auth.starttls.enable: true
代码
package com.example.mailexcel;

import com.alibaba.excel.EasyExcel;
import lombok.SneakyThrows;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.MimeMessage;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/mail")
public class MailController {

    private final JavaMailSender javaMailSender;

    private final MailProperties mailProperties;

    public MailController(JavaMailSender javaMailSender, 
                          MailProperties mailProperties) {
        this.javaMailSender = javaMailSender;
        this.mailProperties = mailProperties;
    }

    @PostMapping("/send")
    public String send() {
        byte[] bytes = excelToByteArray(data());
        email(bytes);
        return "OK";
    }

    @SneakyThrows
    private void email(byte[] attachment) {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(mailProperties.getUsername());
        helper.setTo("xxx@xxx.com");
        helper.setText("内容:测试");
        helper.setSubject("主题:测试");
        // 附件
        helper.addAttachment("测试.xlsx", new ByteArrayResource(attachment));
        javaMailSender.send(message);
    }

    
    private byte[] excelToByteArray(List list) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        EasyExcel.write(outputStream, User.class).sheet("测试").doWrite(list);
        return outputStream.toByteArray();
    }

    
    public static List data() {
        List list = new ArrayList<>();
        list.add(new User(1, "小明", 18));
        list.add(new User(2, "小红", 20));
        list.add(new User(3, "小华", 25));
        return list;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3977161.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-21
下一篇 2022-10-21

发表评论

登录后才能评论

评论列表(0条)

保存