Javamail发送Excel附件具体实现

在我写大学生高考志愿填报的时候,将推荐出来的专业表的信息发送到指定的账户的邮件中

下面代码的实现讲解:

  • 首先创建配置文件,配置邮箱账户的信息
  • 配置用于生成表格的实体类,实体类中的信息就对应着Excel表中的信息
  • 逻辑的具体实现:
    • 首先从Cookie中获取账户的邮箱信息,(我在cookie中只存了账户的邮箱号,没有帐户其他的信息)。
    • 获取我查询结果的datas
    • 将datas封装成Excel表格
    • 发送带有Excel表格的邮件
    • 将本地的Excel表格删除掉
application.yml:
java 复制代码
spring:
  mail:
    host: smtp.qq.com
    username: [email protected]
    password: asdoighwakjfns
    properties:
      mail:
        smtp:
          ssl:
            enable: true
entity层:(实体类中的血法)
java 复制代码
@Getter
@Setter
@TableName("t_pitch")
public class Pitch implements Serializable {
    private static final long serialVersionUID = 1L;
    @ExcelIgnore
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    /**
     * 院校代号
     */
    @ExcelProperty("院校代号")
    @TableField("schoolCode")
    private String schoolCode;
    /**
     * 学校名称
     */
    @ExcelProperty("学校名称")
    @TableField("schoolName")
    private String schoolName;
    /**
     * 专业代号
     */
    @ExcelProperty("专业代号")
    @TableField("pCode")
    private String pCode;
    /**
     * 专业名称
     */
    @ExcelProperty("专业名称")
    @TableField("pName")
    private String pName;
    /**
     * 最低投档分数
     */
    @ExcelProperty("最低投档分数")
    @TableField("lowestScore")
    private String lowestScore;
    /**
     * 最低投档位次
     */
    @ExcelProperty("最低投档位次")
    @TableField("lowestRank")
    private String lowestRank;
}
controller层代码:
java 复制代码
@RequestMapping("/sendExcel")
@ResponseBody
public void sendExcel(HttpServletRequest request, HttpServletResponse response) throws MessagingException {
    //1、获取cookie中的邮箱信息
    Cookie[] cookies = request.getCookies();
    for(Cookie cookie:cookies){
        if(cookie.getName().equals("email")){
            email1 = cookie.getValue();
            break;
        }
    }
    //2、获取表中的信息 datas,然后生成xlsx
    //写文件:
    String tempFilePath=PATH+"专业推荐表.xlsx";
    EasyExcel.write(tempFilePath,Pitch.class)
            .sheet("统计表1")
            .doWrite(datas);
    //3、发送信息
    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message,true);
    helper.setFrom("[email protected]");
    helper.setTo(email1);
    helper.setSubject("邮件发送数据");
    helper.setText("以下是为您导出的专业推荐表");

    File file = new File(tempFilePath);
    DataSource source= new FileDataSource(file);
    helper.addAttachment(file.getName(),source);
    javaMailSender.send(message);
    boolean delete = file.delete();
    if (delete) {
        System.out.println("Temporary file deleted successfully.");
    } else {
        System.out.println("Failed to delete temporary file.");
    }


}
相关推荐
kill bert3 小时前
Java八股文背诵 第四天JVM
java·开发语言·jvm
z2014z3 小时前
Excel 自动执行全局宏
excel
uhakadotcom5 小时前
RunPod:AI云计算的强大助手
后端·面试·github
Pitayafruit5 小时前
📌 Java 工程师进阶必备:Spring Boot 3 + Netty 构建高并发即时通讯服务
spring boot·后端·netty
uhakadotcom5 小时前
Google AlloyDB AI 与 PostgreSQL 的核心区别
后端·面试·github
uhakadotcom5 小时前
使用Go语言编写简单爬虫程序
后端·面试·github
梦想实现家_Z5 小时前
SpringBoot实现MCP Server实战详解
spring boot·后端·mcp
你是理想6 小时前
wait 和notify ,notifyAll,sleep
java·开发语言·jvm
helloworld工程师6 小时前
【微服务】SpringBoot整合LangChain4j 操作AI大模型实战详解
java·eclipse·tomcat·maven
Java&Develop6 小时前
idea里面不能运行 node 命令 cmd 里面可以运行咋回事啊
java·ide·intellij-idea