springboot项目打包jar发布项目,不能发送freemarker模板邮件

问题:

    springboot项目使用JavaMailSender在开发工具启动项目可以正常发送邮件,包括模板邮件,

    但是打包jar部署项目就不可以发送模板邮件。

项目地址:项目代码仓库,Email代码具体位置

出现问题代码:(只是不可以打包jar部署项目发送模板邮件,其他均正常)

package com.luck.email;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import javax.mail.internet.MimeMessage;
import java.util.Map;
import static com.luck.email.MailConfig.FROM_MAIL;
@Component
public class MailService2{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private JavaMailSender mailSender;
    public void sendMailTemplate(String to, String subject, String templatePath, Map map){
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(FROM_MAIL);
            helper.setTo(to);
            helper.setSubject(subject);

            Configuration cfg = new Configuration(Configuration.VERSION_2_3_27);
            cfg.setClassLoaderForTemplateLoading(ClassLoader.getSystemClassLoader(),"/templates/ftl");
            Template emailTemplate = cfg.getTemplate(templatePath);

            String text = FreeMarkerTemplateUtils.processTemplateIntoString(emailTemplate, map);
            helper.setText(text, true);
            mailSender.send(message);
            logger.info("-----email-----模板邮件发送成功.");
        } catch (Exception e){
            e.printStackTrace();
            logger.info("-----email-----模板邮件发送失败.");
        }
    }
}

修改之后代码:(打包jar部署项目,也是可以发送模板邮件)

package com.luck.email;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.internet.MimeMessage;
import java.util.Map;
import static com.luck.email.MailConfig.FROM_MAIL;
@Component
public class MailService2{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private JavaMailSender mailSender;
    // 1.添加注入FreeMarkerConfigurer 
    @Autowired
    private FreeMarkerConfigurer configurer;
    public void sendMailTemplate(String to, String subject, String templatePath, Map map){
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(FROM_MAIL);
            helper.setTo(to);
            helper.setSubject(subject);

            /*Configuration cfg = new Configuration(Configuration.VERSION_2_3_27);
            cfg.setClassLoaderForTemplateLoading(ClassLoader.getSystemClassLoader(),"/templates/ftl");
            Template emailTemplate = cfg.getTemplate(templatePath);*/
            // 2.或许模板文件改为这种方式
            Template emailTemplate = configurer.getConfiguration().getTemplate(templatePath);

            String text = FreeMarkerTemplateUtils.processTemplateIntoString(emailTemplate, map);
            helper.setText(text, true);
            mailSender.send(message);
            logger.info("-----email-----模板邮件发送成功.");
        } catch (Exception e){
            e.printStackTrace();
            logger.info("-----email-----模板邮件发送失败.");
        }
    }
}

  注意:.ftl文件放在templates目录下.

---------------------------------------------------------------------------------------------------------------------------------------

总结:解决办法注入 FreeMarkerConfigurer类,并获取Template 。

原因:目前不清楚。

你可能感兴趣的:(-spring-boot)