springBoot中的邮件发送

1. 添加依赖

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2. impl

 

 public String sendWord() {
        User user1 = new User();
        user1.setId("1");
        user1.setUsername("李四");
        user1.setSex("男");
        User user2 = new User();
        user2.setId("2");
        user2.setUsername("小王");
        user2.setSex("女");
        User user3 = new User();
        user3.setId("3");
        user3.setUsername("张三");
        user3.setSex("男");
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        try {
            Workbook wb = new SXSSFWorkbook(500);
            CellStyle style = wb.createCellStyle();
            Sheet sheet = wb.createSheet();
            sheet.setColumnWidth(0, 6200);
            sheet.setColumnWidth(1, 3800);
            sheet.setColumnWidth(2, 6200);
            for (int i = 3; i <= 11; i++) {
                sheet.setColumnWidth(i, 3766);

            }
            Row row = sheet.createRow(0);
            row.setRowStyle(style);
            Cell cell0 = row.createCell(0);
            cell0.setCellStyle(style);
            cell0.setCellValue("id");

            Cell cell1 = row.createCell(1);
            cell1.setCellStyle(style);
            cell1.setCellValue("姓名");

            Cell cell2 = row.createCell(2);
            cell2.setCellStyle(style);
            cell2.setCellValue("性别");
            int rownum = 1;
            CellStyle cellStyle = wb.createCellStyle();
            for (User user : list) {
                Row r = sheet.createRow(rownum);
                r.createCell(0);
                Cell c0 = r.createCell(0);
                c0.setCellStyle(cellStyle);
                c0.setCellValue(user.getId());
                Cell c1 = r.createCell(1);
                c1.setCellStyle(cellStyle);
                c1.setCellValue(user.getUsername());
                Cell c2 = r.createCell(2);
                c2.setCellStyle(cellStyle);
                c2.setCellValue(user.getSex());
                rownum++;
            }
            // wb.write(out);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            wb.write(baos);
            baos.flush();
            byte[] bt = baos.toByteArray();
            InputStream is = new ByteArrayInputStream(bt, 0, bt.length);
            baos.close();
            String email = "接收人的@qq.com";
            sendMail("保险产品审核推送表", email, "邮件正文", is);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    /**
     *
     * @param subject 邮件的主题
     * @param toMail  接收人的邮箱
     * @param content 邮件的正文
     * @param is 发送的附件为流内容
     * @return
     */
    public static boolean sendMail(String subject, String toMail, String content, InputStream is) {
        boolean isFlag = false;
        try {
            String smtpFromMail = "xxxx@163.com.cn";  //账号
            String pwd = "password"; //密码
            int port = 25; //端口
            String host = "smtp.163.com"; //邮件服务器地址

            Properties props = new Properties();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", "true");
            Session session = Session.getDefaultInstance(props);
            session.setDebug(false);

            MimeMessage message = new MimeMessage(session);
            try {
                message.setFrom(new InternetAddress(smtpFromMail, "发件人的名称"));
                /*批量发送*/
//                InternetAddress[] adc = {new InternetAddress("1067584215@qq.com"), new InternetAddress("1436495697@qq.com")};
//                message.addRecipients(Message.RecipientType.TO, adc);
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(toMail));
                message.setSubject(subject);
                message.addHeader("charset", "UTF-8");

                /*添加正文内容*/
                Multipart multipart = new MimeMultipart();
                BodyPart contentPart = new MimeBodyPart();
                contentPart.setText(content);

                contentPart.setHeader("Content-Type", "text/html; charset=UTF-8");
                multipart.addBodyPart(contentPart);

                /*添加附件*/
                MimeBodyPart fileBody = new MimeBodyPart();
                DataSource source = new ByteArrayDataSource(is, "application/msexcel");
                fileBody.setDataHandler(new DataHandler(source));
                String fileName = "附件.xlsx";
                // 中文乱码问题
                fileBody.setFileName(MimeUtility.encodeText(fileName));
                multipart.addBodyPart(fileBody);

                message.setContent(multipart);
                message.setSentDate(new Date());
                message.saveChanges();
                Transport transport = session.getTransport("smtp");

                transport.connect(host, port, smtpFromMail, pwd);
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
                isFlag = true;
            } catch (Exception e) {
                e.printStackTrace();
                isFlag = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isFlag;
    }

3. yml配置

server:
  port: 8081
spring:
  mail:
    host: smtp.163.com
    username: xxx@163.com
    password: password
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true
posted @ 2020-01-01 21:30  这都没什么  阅读(253)  评论(0编辑  收藏  举报