需要的Jar包
<!--邮件发送-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
工具类
package cn.lineon.reggie.utils;
import lombok.extern.slf4j.Slf4j;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.Properties;
@Slf4j
public class EmailUtils {
private static final String USEREMAIL = "system@lineon.cn"; // 发件人称号,同邮箱地址
private static final String PASSWORD = ""; // 可以使户端授权码
/**
* 发送邮件
* @param to
* @param text
* @param title
* @return
*/
public static boolean sendMail(String to, String text, String title){
try {
final Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.exmail.qq.com");//发送服务器,根据实际填写
props.put("mail.user", USEREMAIL);
props.put("mail.password", PASSWORD);
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
String username = props.getProperty("mail.user");
InternetAddress form = new InternetAddress(username);
message.setFrom(form);
// 设置收件人
InternetAddress toAddress = new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO, toAddress);
// 设置邮件标题
message.setSubject(title);
// 设置邮件的内容体
message.setContent(text, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
return true;
}catch (Exception e){
e.printStackTrace();
}
return false;
}
/**
* 小小的测试下
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//加载邮件html模板
String fileName = "emailTemplate/validate.html";
InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName);
BufferedReader fileReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder emailTemplate = new StringBuilder();
String line = "";
try {
while ((line = fileReader.readLine()) != null) {
emailTemplate.append(line);
}
} catch (Exception e) {
log.error("读取文件失败,fileName:{}", fileName, e);
} finally {
inputStream.close();
fileReader.close();
}
//格式换填充数据
String formatTemplate = MessageFormat.format(emailTemplate.toString(), 1010);
EmailUtils.sendMail("2801941655@qq.com",formatTemplate,"蓝溪-验证码");
}
}
邮件模板
<div style="background:#ecf1f3;padding-top:20px; min-width:820px;">
<div style="width:801px;height:auto; margin:0px auto;">
<div style="width:778px;height:auto;margin:0px 11px;background:#fff;box-shadow: 6px 3px 5px rgba(0,0,0,0.05);-webkit-box-shadow: 6px 3px 5px rgba(0,0,0,0.05);-moz-box-shadow: 6px 3px 5px rgba(0,0,0,0.05);-ms-box-shadow: 6px 3px 5px rgba(0,0,0,0.05);-o-box-shadow: 6px 3px 5px rgba(0,0,0,0.05);">
<div style="width:781px;height:160px; background:#fff;">
<div style="width:200px;height:160px;background:url(https://blog.lineon.cn/wp-content/uploads/2022/06/1654689591-96d6f2e7e1f705a.png) 0px 60px no-repeat; margin:0px auto;background-size: contain;"></div>
</div>
<div style="width:627px;margin:0 auto; padding-left:77px; background:#fff;font-size:14px;color:#55798d;padding-right:77px;">
<br>
<div style="overflow-wrap:break-word;line-height:30px;">
验证码:{0}
</div>
<br><br><br>
</div>
</div>
<div style="position:relative;top:-15px;width:800px;height: 360px;background:url(https://blog.lineon.cn/wp-content/themes/ripro-v2/assets/img/mail-bg.jpg) 0px 0px no-repeat;">
<div style="height:200px;color:#507383;font-size:14px;line-height: 1.4;padding: 20px 92px;">
<div style="font-size: 22px;font-weight: bold;"><a href="https://blog.lineon.cn">蓝上小栈</a></div>
<div style="margin:20px 0;color: #6a8895;min-height:4.2em;white-space: pre-wrap;">一个吐槽、偶尔提供一些源码分享、发布教程文章。致力于为广大小白站长、编程爱好者提供学习交流的资源,以减轻学习的难度。</div>
<div style="">此邮件为系统通知邮件,切勿直接回复</div>
</div>
<div style="clear:both;"></div>
</div>
</div>
</div>
声明:若发现资源失效或错误可在文章顶部“评论建议”留言,我们将尽快处理!本站绝大部分资源都来自互联网,原作者信息已无从考究。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。