Hutool发送html格式邮件


提示:以下是本篇文章正文内容,下面案例可供参考

一、pom依赖包

<dependency>
   <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.12</version>
</dependency>

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

二、定时任务,发送邮件。

代码如下(示例):

import cn.deling.common.enums.MailEnum;
import cn.deling.device.entity.dataobject.TuyaAuthDTO;
import cn.deling.device.service.AutoAuthorizationService;
import cn.hutool.extra.mail.MailUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Component
@EnableScheduling
@Slf4j
public class TuyaAuthMailTask {

    @Resource
    private AutoAuthorizationService autoAuthorizationService;

    //@Scheduled(cron ="0/40 * * * * ?")  // 每40s 执行一次。
    @Scheduled(cron ="0 0 9 * * ?")  // 每天9点执行一次
    public void TuyaAuthMail() {
        log.error("=====================================================定时任务触发============================================================");
        List<TuyaAuthDTO> tuyaAuthList = autoAuthorizationService.queryAllTuyaAuthMail();
        MailUtil.send("ryan2103@qq.com", "涂鸦PID授权余量,通知!", SenMailHTML(tuyaAuthList), true);

    }

    private String SenMailHTML(List<TuyaAuthDTO> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            // Excel内容在邮件正文显示
            StringBuilder htmlText = new StringBuilder();
            String htmlStar = "<html><body><table border=\"1px\" cellpadding=\"10px\">";
            String htmlEnd = "</table></body></html>";
            // 表头
            String th = "<tr><th>PID类型</th>" +
                        "<th>产品PID</th>" +
                        "<th>PID剩余总量</th></tr>";
            // 行数据
            StringBuilder tr = new StringBuilder();

            // 遍历集合,封装table行数据。
            for (int i = 0; i < list.size(); i++) {
                // 只展示前20条数据
                if (i == 20) {
                    tr.append("更多数据请打开Excel附件查看详情O(∩_∩)O~");
                    break;
                }
                tr.append(
                        "<tr>" +
                            "<td>" + list.get(i).getPidType() + "</td>" +
                            "<td>" + list.get(i).getPid() + "</td>" +
                            "<td>" + list.get(i).getUnusedCount() + "</td>" +
                        "</tr>"
                );
            }

            // 拼接HTML页面
            htmlText.append(htmlStar);
            htmlText.append(th);
            htmlText.append(tr);
            htmlText.append(htmlEnd);
            return String.valueOf(htmlText);
        }
        return "PID权限查询列表List集合,为空!!!";
    }
}
 // 遍历集合,封装table行数据。
            list.stream().peek(item -> {
                int size = item.getProjectName().size();
                if (size > 1) {
                    Stream.iterate(0, num -> num +1).limit(size).forEach( a-> {
                        if (a == 0) {
                            tr.append(
                                    "<tr>" +
                                        "<td>" + item.getProjectName().get(a) + "</td>" +
                                        "<td rowspan = "+ item.getProjectName().size() +">" + item.getPid() + "</td>" +
                                        "<td rowspan = "+ item.getProjectName().size() +">" + item.getPidType() + "</td>" +
                                        "<td rowspan = "+ item.getProjectName().size() +">" + item.getUnusedCount() + "</td>" +
                                    "</tr>"
                            );
                        } else {
                            tr.append(
                                    "<tr >" +
                                            "<td>" + item.getProjectName().get(a)+ "</td>" +
                                    "</tr>"
                            );
                        }
                    });
                } else {
                    tr.append(
                            "<tr>" +
                                    "<td>" + item.getProjectName().get(0) + "</td>" +
                                    "<td>" + item.getPid() + "</td>" +
                                    "<td>" + item.getPidType() + "</td>" +
                                    "<td>" + item.getUnusedCount() + "</td>" +
                            "</tr>"
                    );
                }
            }).collect(Collectors.toList());

引用来源地址

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值