1. 程式人生 > >springboot專案打包jar釋出專案,不能傳送freemarker模板郵件

springboot專案打包jar釋出專案,不能傳送freemarker模板郵件

問題:

    springboot專案使用JavaMailSender在開發工具啟動專案可以正常傳送郵件,包括模板郵件,

    但是打包jar部署專案就不可以傳送模板郵件。

出現問題程式碼:(只是不可以打包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 。

原因:目前不清楚。