java邮件发送

授权、编写、发送

spring:
  mail:
    username: [email protected]
    password: 123456123456
    host: smtp.qq.com
    properties:
      mail:
        smtp:
          ssl:
            enable: true

导包

    
            org.springframework.boot
            spring-boot-starter-mail
        
 @RequestMapping("mail")
    public String mail(MultipartFile file, HttpServletResponse response) throws IOException, MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setSubject("标题");
        helper.setText("文字");
        helper.setFrom("[email protected]");
        helper.setTo("[email protected]");
        //创建文件的方法,
        String excile = createExcile(null);
        //文件
        helper.addAttachment("附件.xlsx", new File(excile));
        mailSender.send(message);
        return "ok";
    }

 private String createExcile(HttpServletResponse response) throws IOException {
        DemoData d1 = new DemoData("1", "qx1", "11", new Date());
        DemoData d2 = new DemoData("1", "qx1", "12", new Date());
        DemoData d3 = new DemoData("1", "qx1", "13", new Date());
        DemoData d4 = new DemoData("1", "qx1", "14", new Date());
        DemoData d5 = new DemoData("1", "qx1", "15", new Date());
        DemoData d11 = new DemoData("3", "3", "21", new Date());
        DemoData d6 = new DemoData("2", "2", "16", new Date());
        DemoData d7 = new DemoData("2", "2", "17", new Date());
        DemoData d8 = new DemoData("2", "2", "18", new Date());
        DemoData d9 = new DemoData("2", "2", "19", new Date());
        DemoData d10 = new DemoData("2", "2", "20", new Date());
        DemoData d12 = new DemoData("4", "4", "22", new Date());
        ArrayList list = new ArrayList<>();
        list.add(d1);
        list.add(d2);
        list.add(d3);
        list.add(d4);
        list.add(d5);
        list.add(d11);
        list.add(d6);
        list.add(d7);
        list.add(d8);
        list.add(d9);
        list.add(d10);
        list.add(d12);
        String name = "D:\\我的电脑\\fsdownload\\" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(name, DemoData.class).sheet("模板").doWrite(list);
        return name;
    }

 

你可能感兴趣的:(java,服务器,开发语言)