package com.royalrangers.service;

import com.royalrangers.model.User;
import freemarker.template.Configuration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class EmailService {

    @Autowired
    JavaMailSender mailSender;

    @Autowired
    Configuration freeMarkerConfiguration;

    public void sendEmail(User user, String subj, String template, String message) {

        MimeMessagePreparator preparator = getMessagePreparator(user, subj, template, message);
        try {
            mailSender.send(preparator);
        } catch (MailException e) {
            log.error("MailException", e.getMessage());
        }
    }

    private MimeMessagePreparator getMessagePreparator(User user, String subj, String template, String message) {

        MimeMessagePreparator preparator = new MimeMessagePreparator() {

            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

                helper.setSubject(subj);
                helper.setFrom("no-reply@royalrangers.com");
                helper.setTo(user.getEmail());

                Map<String, Object> model = new HashMap<String, Object>();
                model.put("email", user.getEmail());
                model.put("message", message);

                String text = getFreeMarkerTemplateContent(model, template);
                helper.setText(text, true);
            }
        };
        return preparator;
    }

    public String getFreeMarkerTemplateContent(Map<String, Object> model, String template) {
        StringBuffer content = new StringBuffer();
        try {
            content.append(FreeMarkerTemplateUtils.processTemplateIntoString(
                    freeMarkerConfiguration.getTemplate(template), model));
            return content.toString();
        } catch (Exception e) {
            log.error("Exception occured while processing Emailtemplate:", e.getMessage());
        }
        return "";
    }

}

@@ -549,48 +549,8 @@
 s;%0A%0A
-import javax.mail.internet.MimeMessage;%0A
 impo
@@ -1253,119 +1253,25 @@
 r = 
-new MimeMessagePreparator() %7B%0A%0A            public void prepare(MimeMessage mimeMessage) throws Exception %7B%0A    
+mimeMessage -%3E %7B%0A
     
@@ -1361,36 +1361,32 @@
 );%0A%0A            
-    
 helper.setSubjec
@@ -1398,73 +1398,8 @@
 j);%0A
-                helper.setFrom(%22no-reply@royalrangers.com%22);%0A    
     
@@ -1434,28 +1434,24 @@
 tEmail());%0A%0A
-    
             
@@ -1494,31 +1494,13 @@
 Map%3C
-String, Object
 %3E();%0A
-    
     
@@ -1548,36 +1548,32 @@
 ));%0A            
-    
 model.put(%22messa
@@ -1584,28 +1584,24 @@
  message);%0A%0A
-    
             
@@ -1665,36 +1665,32 @@
 e);%0A            
-    
 helper.setText(t
@@ -1705,22 +1705,8 @@
 e);%0A
-            %7D%0A
     
@@ -1712,16 +1712,17 @@
      %7D;%0A
+%0A
         
@@ -1836,24 +1836,25 @@
 template) %7B%0A
+%0A
         Stri
@@ -2229,24 +2229,25 @@
 ;%0A        %7D%0A
+%0A
         retu

Use lambdas in EmailService.