To send emails using FreeMarker templates in a Spring Boot application, you can follow these steps. I’ll provide a high-level overview and then go into the details of each

Step.1. Set Up Spring Boot Application
First, create a Spring Boot application if you don’t have one already.

2. Add Dependencies
Add the necessary dependencies in your pom.xml for Spring Boot, JavaMail, and FreeMarker.

3. Configure Email Properties
Set up the email server properties in your application.properties or application.yml.

4. Create FreeMarker Template
Create an HTML file in the src/main/resources/templates directory.

5. Service to Send Email
Create a service to send the email using JavaMailSender and FreeMarker.

6. Controller (Optional)
Create a controller to handle email sending requests if needed.Detailed Steps:

1. Set Up Spring Boot Application
You can create a new Spring Boot application using Spring Initializr. Select the dependencies for Spring Web, Spring Mail, and FreeMarker.

2. Add Dependencies
In your pom.xml, include the following dependencies:xml
Copy code
<dependencies>
    <!– Spring Boot Starter Mail –>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>    <!– FreeMarker –>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>    <!– Other dependencies like Spring Web, etc. –>
</dependencies>
3. Configure Email Properties
In application.properties or application.yml, configure your email settings:properties
Copy code
spring.mail.host=smtp.example.com
spring.mail.port=587
spring.mail.username=your-email@example.com
spring.mail.password=your-email-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
4. Create FreeMarker Template
In src/main/resources/templates, create an HTML file, e.g., email-template.ftl:html
Copy code
<!DOCTYPE html>
<html>
<head>
    <title>Email Template</title>
</head>
<body>
    <h1>${subject}</h1>
    <p>${message}</p>
</body>
</html>
5. Service to Send Email
Create a service class to handle the email sending logic:java
Copy code
package com.example.demo.service;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 freemarker.template.Configuration;
import freemarker.template.Template;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Map;

@Service
public class EmailService {   

 @Autowired
    private JavaMailSender mailSender;    

@Autowired
    private Configuration freemarkerConfig;    

public void sendEmail(String to, String subject,Map<String, Object> model) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();      

  try {
  MimeMessageHelper helper = newMimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setSubject(subject);       

     Template template = freemarkerConfig.getTemplate(“email-template.ftl”);

            String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);          

             helper.setText(html, true);          

             mailSender.send(message);
        } catch (MessagingException e) {
            throw new Exception(“Failed to send email”, e);
        }
    }
}
6. Controller (Optional)
If you want to expose an endpoint to send emails, create a controller:java
Copy code
package com.example.demo.controller;import com.example.demo.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(“/api/emails”)
public class EmailController {    

@Autowired
    private EmailService emailService;    

@PostMapping(“/send”)
    public String sendEmail(@RequestParam String to, @RequestParam String subject, @RequestParam String message) {
        try {
            Map<String, Object> model = new HashMap<>();
            model.put(“subject”, subject);
            model.put(“message”, message);           emailService.sendEmail(to, subject, model);
            return “Email sent successfully!”;
        } catch (Exception e) {
            return “Failed to send email: ” + e.getMessage();
        }
    }
}
Conclusion
This is a basic implementation to send emails using FreeMarker templates in a Spring Boot application. You can customize the template, service, and controller as per your needs. Additionally, ensure that your email credentials and server configurations are set correctly.