SpringBoot - HuTool 发邮件/批量发送

1.引入maven依赖

  <!--HuTool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.0</version>
        </dependency>

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

2.配置

resources下建立config文件夹中创建名为mail.setting文件

 

 

 

 

 特别注意

如果使用foxmail邮箱,此处user为qq号

# 邮件服务器的SMTP地址
host = smtp.qq.com
# 邮件服务器的SMTP端口
port = 465
# 发件人(必须正确,否则发送失败)
from = 1181055xxx@qq.com
# 用户名(注意:如果使用foxmail邮箱,此处user为qq号)!!!!
user = 1181055xxx
# 密码(注意,某些邮箱需要为SMTP服务单独设置密码,详情查看相关帮助)
pass = zbononvcbhcxxxxx
#使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。
startttlsEnable = true
# 使用SSL安全连接
sslEnable = true
# 指定实现javax.net.SocketFactory接口的类的名称,这个类将被用于创建SMTP的套接字
socketFactoryClass = javax.net.ssl.SSLSocketFactory
# 如果设置为true,未能创建一个套接字使用指定的套接字工厂类将导致使用java.net.Socket创建的套接字类, 默认值为true
socketFactoryFallback = true
# 指定的端口连接到在使用指定的套接字工厂。如果没有设置,将使用默认端口456
socketFactoryPort = 465
# SMTP超时时长,单位毫秒,缺省值不超时
timeout = 0
# Socket连接超时值,单位毫秒,缺省值不超时
connectionTimeout = 0

3.测试发送

@RestController
public class TestEmailController {
    @RequestMapping("/email")
    public void email(){
        //批量发送
        ArrayList<String> emailList = CollUtil.newArrayList();
        emailList.add("gxqxxx12@163.com");
        emailList.add("1181055xxx@qq.com");
        String stringEmail = getStringEmail();
        //单个发送-false非HTML 格式-----true HTML 格式
//        MailUtil.send("gxqxxx12@163.com", "测试", "邮件测试发送", false);
        MailUtil.send(emailList, "测试", stringEmail, true);
        System.out.println("测试");
    }
    public String getStringEmail(){
        return  "<html>\n" +
                "<style>\n" +
                "    .td{width:150px;height:70px}\n" +
                "</style>\n" +
                "<body>\n" +
                "<table border=\"1\">\n" +
                "    <tr>\n" +
                "        <td style=\"text-align:center\" class=\"td\">序号</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">范围</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">姓名</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">员工类别</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">出勤天数</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">月薪</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">基本工资</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">绩效</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">岗位工资</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">住房津贴</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">生活福利</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">工龄工资</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">话补</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">餐补</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">车补</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">工龄工资</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">其他奖金</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">全勤奖</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">事假扣除</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">迟到扣除</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">社保</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">公积金</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">本月缴纳个税</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">实发工资</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">日期</td>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+1+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+2+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+3+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+4+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+5+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+6+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+7+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+8+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+9+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+10+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+11+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+12+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+13+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+14+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+15+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+16+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+17+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+18+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+19+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+20+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+21+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+22+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+23+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+24+"</td>\n" +
                "        <td style=\"text-align:center\" class=\"td\">"+25+"</td>\n" +
                "    </tr>\n" +
                "</table>\n" +
                "</body>\n" +
                "</html>\n";
    }
}

 

posted @ 2022-08-30 16:13  RunawayProgrammer  阅读(627)  评论(0编辑  收藏  举报