使用email发送exec

该代码片段展示了如何在Java项目中使用EasyExcel库处理数据并将之转换为Excel文件,然后通过自定义的sendFileEmail方法利用ApachePOI和邮件服务发送带有附件的电子邮件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用到的依赖:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.3.4</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>

代码

    public void importVehicleInfoExec() throws Exception {
        List<VehicleDailyData> vehicleDailyDataList = vehicleService.getVehicleDailyDateList();
        ByteArrayOutputStream out = null;
        try {
            // 返回excel文件流
            out = ExcelUtil.generateExcel(vehicleDailyDataList, VehicleDailyData.class);
            LOG.debug("开始发送邮件===========================");
            LOG.debug("开始发送邮件===========================");
            LOG.debug("开始发送邮件===========================");
            SendMailUtil.sendFileEmail("车辆日常运行报表",out,"2039701916@qq.com",mailProperties);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (MailException e) {
            throw new RuntimeException(e);
        } finally {
            if(out != null){
                out.close();
            }
        }
    }

public class ExcelUtil {

    public static <T> ByteArrayOutputStream generateExcel(List<T> dataList, Class<T> clazz) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        // excel写入
        EasyExcel.write(out,clazz).sheet(0).doWrite(dataList);
        return out;
    }

}
    public static void sendFileEmail(String subject, ByteArrayOutputStream outputStream, String to, MailProperties mailProperties) throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.host", mailProperties.getHost());
        prop.setProperty("mail.transport.protocol", mailProperties.getProtocol());
        prop.setProperty("mail.smtp.auth", "true");
        prop.setProperty("mail.smtp.ssl.enable", "true");
        Session session = Session.getInstance(prop);
        Transport ts = session.getTransport();
        ts.connect(mailProperties.getHost(), mailProperties.getUsername(), mailProperties.getPassword());
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(mailProperties.getUsername()));
        message.setRecipients(RecipientType.TO, StringUtils.isNotBlank(to) ? to : "chkai.good@163.com");
        message.setSubject(subject);
        MimeBodyPart mbp = new MimeBodyPart();
        DataSource source = new ByteArrayDataSource(outputStream.toByteArray(), "application/msexcel");
        mbp.setDataHandler(new DataHandler(source));
        mbp.setFileName(MimeUtility.encodeText(subject + ".xls"));
        MimeMultipart multipart = new MimeMultipart();
        multipart.addBodyPart(mbp);
        message.setContent(multipart);
        ts.sendMessage(message, message.getAllRecipients());
        ts.close();
    }
mailProperties对象 是我们自己写的一个配置类,里面有自己的邮箱,密码等信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值