添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
application.properties中添加配置
##邮件配置
spring.mail.host=smtp.exmail.qq.com
spring.mail.port=465
spring.mail.username=adc@qq.com
spring.mail.password=123456
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.default-encoding=UTF-8
## Freemarker 配置
spring.freemarker.allow-request-override=false
spring.freemarker.allow-session-override=false
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.prefer-file-system-access=true
# 这是重点, 会过滤.ftl后缀的文件
spring.freemarker.suffix=.ftl
# spring boot 默认的页面模板存放目录
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.settings.template_update_delay=0
spring.freemarker.settings.default_encoding=UTF-8
spring.freemarker.settings.classic_compatible=true

image
发送邮件工具类
@Component
public class MailUtil {
private String sender;
@Autowired
private JavaMailSender mailSender;
private static final Logger logger = LoggerFactory.getLogger(MailUtil.class);
public void sendMail(String receiver, String title, String text, Boolean isHtml) throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(sender);
helper.setTo(receiver);
helper.setSubject(title);
helper.setText(text, isHtml);
mailSender.send(mimeMessage);
}
}
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class HelloController {
@Autowired
private MailUtil mailUtil;
@Autowired
private Configuration configuration;
@GetMapping("/index")
public void execNewsInfo()throws Exception{
Map<String, Object> model = new HashMap();
List<NewsVo> newsList = new ArrayList<>();
for (Integer i = 0;i<10;i ++){
NewsVo po = new NewsVo();
po.setTitle("测试标题__" + i);
po.setRefId(String.valueOf(i));
po.setLink("https://baidu.com/?p=" + i );
newsList.add(po);
}
model.put("newsList", newsList);
Template template = configuration.getTemplate("mail.ftl");
String htmlStr = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
mailUtil.sendMail("admin.qq.com", "新闻列表", htmlStr, true);
}
}
mail.ftl 模板详情
<html>
<body>
<h3>新闻列表:</h3>
<table border="1px solid #8968CD" style="border-collapse: collapse;">
<tr>
<th>新闻ID</th>
<th>新闻标题</th>
<th>新闻链接</th>
</tr>
<#list newsList as news>
<tr>
<td>${news.refId}</td>
<td>${news.title}</td>
<td><a href="${news.link}" target="_blank">查看详情</a></td>
</tr>
</#list>
</table>
</body>
</html>
网友评论