1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
本次邮箱测试使用了freemarker模板
2.添加配置
在 application.properties 中添加
spring.mail.host=smtp.qq.com
spring.mail.username=********@qq.com
#邮箱授权码
spring.mail.password=授权码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
3.创建JavaMailComponent
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
@Component
@EnableConfigurationProperties(MailProperties.class)
public class JavaMailComponent {
private static final String template = "mail.ftl";
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private MailProperties mailProperties;
public void sendMail(String email) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("email", email);
try {
// 获取内容
String text = this.getTextByTemplate(template, map);
// 发送
this.send(email, text);
} catch (Exception e) {
e.printStackTrace();
}
}
private String getTextByTemplate(String template, Map<String, Object> model) throws Exception {
return FreeMarkerTemplateUtils
.processTemplateIntoString(this.freeMarkerConfigurer.getConfiguration().getTemplate(template), model);
}
private String send(String email, String text) throws MessagingException, UnsupportedEncodingException {
MimeMessage message = this.javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
InternetAddress from = new InternetAddress();
from.setAddress(this.mailProperties.getUsername());
from.setPersonal("Java记", "UTF-8");
helper.setFrom(from);
helper.setTo(email);
helper.setSubject("SpringBoot 发送的第一封邮件");
helper.setText(text, true);
this.javaMailSender.send(message);
return text;
}
}
4.创建邮箱模板
在 src/main/resources 下的 template 目录下创建名为 mail.ftl 的文件,其内容如下:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div style="width: 600px; text-align: left; margin: 0 auto;">
<h1 style="color: #005da7;">Java记</h1>
<div style="border-bottom: 5px solid #005da7; height: 2px; width: 100%;"></div>
<div style="border: 1px solid #005da7; font-size: 16px; line-height: 50px; padding: 20px;">
<div>${email},您好!</div>
<div>
这是个测试
</div>
<div>
想了解更多信息,请访问 <a href="https://www.javaj.work">https://www.javaj.work</a>
</div>
</div>
</div>
</body>
</html>
5.测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {
@Autowired
private JavaMailComponent javaMailComponent;
@Test
public void test() {
this.javaMailComponent.sendMail("********@qq.com");
}
}
运行结果如下:
评论 (0)