Preface: HuTool is a very powerful tool category, which contains many commonly used tools, which can be viewed on the official website .
One, import pom
<!--邮件发送使用的HuTool工具类已经邮件相关pom-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.3.1</version>
</dependency>
Two, tool class writing
import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
/**
* 邮件发送的使用:
* MailUtil.send(account, CollUtil.newArrayList("[email protected]"), subject, content, isHtml);
* account:下方返回的配置账号
* CollUtil.newArrayList("[email protected]"):接收方的邮箱地址(可数组)
* subject:标题
* content:邮件正文,可以是文本,也可以是HTML内容
* isHtml:是否为HTML,如果是,那参数3识别为HTML内容(写file即可)
* eg:
* MailUtil.send(account, CollUtil.newArrayList("[email protected]"), "测试", "邮件来自Hutool测试", false);
*/
public class SendMailTool {
public static MailAccount sendMail(){
MailAccount account = new MailAccount();
account.setHost("smtp.163.com");
account.setPort(25);
account.setAuth(true);
account.setFrom("[email protected]");
account.setUser("liuzy74521");//这里名字需要是发送账户名字
account.setPass("xxxx"); //密码,授权码
return account;
}
}