使用Hutool工具完成发(带模板)邮件功能

刚上班小白公司需求是完成一个邮件发送功能,网上找了一下可以使用Hutool工具的MailUtil完成;费了点时间,最终还是完成了发邮件功能

直接开始正文。

新建一个maven工程,建好的工程目录(idea)
我用的是idea,用eclipse工具的得自己琢磨
分别在resources文件夹下新建template.html(邮件模板)和在子文件夹config下新建一个mail.setting配置文件。
template.html :该文件中{0}、{1}等可以使用MessageFormat替换成我们想要的内容,后面可以看到!

				<!DOCTYPE html>
				<html lang="en">
				<head>
				    <meta charset="UTF-8">
				    <title></title>
				</head>
				<body>
				<div class="info-top" style="padding: 15px 25px;
				                                     border-top-left-radius: 10px;
				                                     border-top-right-radius: 10px;
				                                     background: burlywood;
				                                     color: crimson;
				                                     line-height: 10px;">
				    <div style="font-size: 18px" ><b>信 息 认 证</b></div>
				</div>
				
				<div class="info-wrap" style="border-bottom-left-radius: 10px;
				                                  border-bottom-right-radius: 10px;
				                                  border:1px solid #ddd;
				                                  overflow: hidden;
				                                  padding: 15px 15px 20px;">
				    <div class="tips" style="padding:15px;">
				        <p style=" list-style: 160%; margin: 10px 0;"><h3>Hi</h3>以下信息请您仔细阅读:</p>
				        <p style=" list-style: 160%; margin: 10px 0;">&nbsp;&nbsp;{0}</p>
				    </div>
				    <div class="time" style="text-align: right; color: #999; padding: 0 15px 15px;">{1}</div><br>
				</div>
				</body>
				</html>

mail.setting:

host = 公司邮箱的服务器地址
port = 465
from = 公司邮箱
user = 邮箱用户名
pass = 密码
starttlsEnable = true

sslEnable = true
socketFactoryClass = javax.net.ssl.SSLSocketFactory
socketFactoryFallback = true
socketFactoryPort = 465

timeout = 0
connectionTimeout = 0

配置好上面两个文件之后,先使用输入流的方式读取建好的template.html,将该文件作为邮件模板。再将设置好的内容替换html文件中{0}和{1}。看代码:

//读取到html文件模板,替换文件模板中的信息
    public static String scopeTemplate(){

        String filename = "templates.html";
        InputStream inputStream = ClassLoader.getSystemResourceAsStream(filename);
        BufferedReader fileReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer buffer = new StringBuffer();

        String line = "";
        try{
            while((line = fileReader.readLine()) != null){
                buffer.append(line);
            }
        }catch(IOException e){
            e.printStackTrace();
            System.out.println("read template");
        }finally {
            if (inputStream != null){
                try{
                    inputStream.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            if (fileReader != null){
                try{
                    fileReader.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }

        String contentText = "&nbsp;&nbsp;正文!";
        //落款处需要时间则加上
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(new Date());*/
        String inscription = "❌❌公司";
        String htmlText = MessageFormat.format(buffer.toString(),contentText,inscription);
        return htmlText;
    }

使用MailUtil工具类的send方法发送邮件。这里有两张方式(可以使用写好的实例方式,也可以使用mail.setting配置文件方式。这里我用的就是配置文件的方式) 看方法代码:

 public static boolean sendMail(){
        String theme = "安全邮件";
        MailUtil.send("目的邮箱",theme,scopeTemplate(),true);
        return true;
    }

最后直接使用main方法调用就好了。因为我测试的时候所有的方法是写在同一个类里面的,并且写成了静态方法。所以直接调用就好了。

最后,附上一张测试成功图片,有问题的话欢迎评论指正;
在这里插入图片描述

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值