Springboot Freemaker 模板---发送邮件

添加依赖

        
            org.springframework.boot
            spring-boot-starter-freemarker
        

        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-mail
        
        
            org.projectlombok
            lombok
        
        

application.properties中添加配置


##邮件配置
spring.mail.host=smtp.exmail.qq.com
spring.mail.port=465
[email protected]
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 model = new HashMap();

        List 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 模板详情




新闻列表:

<#list newsList as news>
新闻ID 新闻标题 新闻链接
${news.refId} ${news.title} 查看详情

你可能感兴趣的:(Springboot Freemaker 模板---发送邮件)