Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How use freemarker to send email with springboot

How can i use freemarker to send email with spring-boot? i look in spring-boot examples and dont find anything

I want to generate the body of email with my template

tks

like image 616
Fabio Ebner Avatar asked Jul 31 '15 20:07

Fabio Ebner


2 Answers

There is a "Configuration" Object that you can get as bean:

Here is the code:

package your.package;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import freemarker.template.Configuration;
import freemarker.template.Template;

@Controller
public class MailTemplate {
    @Autowired
    Configuration configuration;

    @RequestMapping("/test")
    public @ResponseBody String test2() throws Exception {

        // prepare data
        Map<String, String> data = new HashMap<>();
        data.put("name", "Max Mustermann");

        // get template
        Template t = configuration.getTemplate("test.html");

        String readyParsedTemplate = FreeMarkerTemplateUtils
                .processTemplateIntoString(t, data);

        // do what ever you want to do with html...

        // just for testing:
        return readyParsedTemplate;

    }

}
like image 80
Jochen Pier Avatar answered Sep 28 '22 02:09

Jochen Pier


First of all, you should define Email content using Freemarker Template, for example

<html> 
<head></head> 
<body>
    <p>Dear ${firstName} ${lastName},</p>
    <p>Sending Email using Spring Boot with <b>FreeMarker template !!!</b></p>
    <p>Thanks</p>
    <p>${signature}</p>
    <p>${location}</p>
</body> 
</html>

Next, creating email service which process email template and returns mine message object, such as

import java.util.Properties;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import com.javabycode.model.Mail;

import freemarker.template.Configuration;
import freemarker.template.Template;

@Service
public class MailService {

    @Autowired
    private JavaMailSender sender;

    @Autowired
    private Configuration freemarkerConfig;


    public void sendEmail(Mail mail) throws Exception {
        MimeMessage message = sender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message);

        // Using a subfolder such as /templates here
        freemarkerConfig.setClassForTemplateLoading(this.getClass(), "/templates");

        Template t = freemarkerConfig.getTemplate("email-template.ftl");
        String text = FreeMarkerTemplateUtils.processTemplateIntoString(t, mail.getModel());

        helper.setTo(mail.getMailTo());
        helper.setText(text, true);
        helper.setSubject(mail.getMailSubject());

        sender.send(message);
    }
}

However, It is not suitable to share full working example here. You can refer to the completed tutorial Spring Boot Freemarker Email Template

Hope this helps!

like image 43
David Pham Avatar answered Sep 28 '22 00:09

David Pham