FileNotFoundException when loading freemarker template in java

后端 未结 5 1234
说谎
说谎 2021-01-04 07:40

I get a file not found exception while loading a freemarker template even though the template is actually present in the path.

Update: This is running as a webservi

5条回答
  •  囚心锁ツ
    2021-01-04 07:48

    this work like a Charm ,

    package tech.service.common;
    
    import freemarker.cache.FileTemplateLoader;
    import freemarker.cache.TemplateLoader;
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.TemplateException;
    import freemarker.template.Version;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.stereotype.Service;
    import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    import java.io.File;
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    import java.util.HashMap;
    import java.util.Map;
    
    @Service
    public class MailingService {
    
    
        @Autowired
        private JavaMailSender sender;
    
    
        public MailResponseDto sendEmail(String mailTo,String Subject) {
            MailResponseDto response = new MailResponseDto();
            MimeMessage message = sender.createMimeMessage();
            Configuration config = new Configuration(new Version(2, 3, 0));
    
            try {
                // set mediaType
                MimeMessageHelper helper = new MimeMessageHelper(message, MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
                        StandardCharsets.UTF_8.name());
                TemplateLoader templateLoader = new FileTemplateLoader(new File("src/main/resources/template"));
                config.setTemplateLoader(templateLoader);
                // add attachment
                helper.addAttachment("logo.png", new File("src/main/resources/static/images/spring.png"));
                Template t = config.getTemplate("email_template_password.ftl");
                Map model = new HashMap<>();
                model.put("Name", "ELAMMARI Soufiane");
                model.put("location", "Casablanca,Morocco");
                String html = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
    
                helper.setTo("example@gmail.com");
                helper.setText(html, true);
                helper.setSubject(Subject);
                sender.send(message);
    
                response.setMessage("mail send to : " + mailTo);
                response.setStatus(Boolean.TRUE);
    
            } catch (MessagingException | IOException | TemplateException e) {
                response.setMessage("Mail Sending failure : "+e.getMessage());
                response.setStatus(Boolean.FALSE);
            }
    
            return response;
        }
    }
    

提交回复
热议问题