Java + Send SMTP Email with Free Marker template

Most of us use Velocity template to create the mail content but in this example, I am going to use the Freemarker template.

Maven Dependencies are given below,


  <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.14</version>
        </dependency>
 <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
 </dependency>

Code:


import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class SendMailer {

    public static void main(String[] args) {

        try {
            //Set the properties
            Properties props = new Properties();
            //Change it with appropriate SMTP host
            props.put("mail.smtp.host", "127.0.0.1");
            props.put("mail.smtp.port", 25);
            Message message = new MimeMessage(Session.getInstance(props, null));
            message.setFrom(new InternetAddress("dkbalachandar@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("dkbalachandar@gmail.com"));
            message.setSubject("Subject");

            // Free marker Template
            Configuration cfg = new Configuration();
            //Assume that the template is available under /src/main/resources/templates
            cfg.setClassForTemplateLoading(SendMailer.class, "/templates/");
            cfg.setDefaultEncoding("UTF-8");
            cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            Template template = cfg.getTemplate("emailTemplate.ftl");

            //Pass custom param values
            Map paramMap = new HashMap();
            paramMap.put("name", "Bala");
            paramMap.put("date", String.valueOf(Calendar.getInstance().getTime()));
            Writer out = new StringWriter();

            template.process(paramMap, out);
            BodyPart body = new MimeBodyPart();
            body.setContent(out.toString(), "text/html");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(body);
            message.setContent(multipart);
            Transport.send(message);
        } catch (MessagingException | IOException | TemplateException e) {
            e.printStackTrace();
        }
    }
}


The below is the Free marker template. Please note that the below is a simple one. But you can define complex content as well.

Free marker template file(emailTemplate.ftl):


<html>
<body style="font-family:calibri">
<p>Thank You ${name}. We have recived your request on ${date} <br>
    We will keep you posted</p>
</body>
</html>