<bean class="org.springframework.mail.javamail.JavaMailSenderImpl" id="mailSender">
  <property name="host" value="${mail.host}"></property>
  <property name="username" value="${mail.username}"></property>
  <property name="password" value="${mail.password}"></property>
  <property name="javaMailProperties">
    <props>
      <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
      <prop key="mail.smtp.port">${mail.smtp.port}</prop>
      <prop key="mail.smtp.host">${mail.host}</prop>
      <prop key="mail.smtp.starttls.enable">true</prop>
    </props>
  </property>
</bean>

 

<bean class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean" id="freemarkerConfiguration">
  <property name="preferFileSystemAccess" value="false"></property>
  <property name="templateLoaderPath" value="classpath:/"></property>
</bean>

 

 

 


 
import java.util.Map;
 
import javax.mail.internet.MimeMessage;
 
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
 
import freemarker.template.Configuration;


 @Service("mailService")
public class MailService {
 @Autowired
  private JavaMailSender javaMailSender;

@Autowired
  private Configuration freemarkerConfiguration;
  
  public void sendMail(final String from, final String to, final String subject, final Map<string object=""> model, final String template){
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
           public void prepare(MimeMessage mimeMessage) throws Exception {
              MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
              message.setFrom(from, "azizkhani");
              message.setTo(to);
              message.setSubject(subject);
              //template sample: "freemarker/confirm-register.ftl"
                String text =FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate(template,"UTF-8"), model);
              message.setText(text, true);
           }
        };
    javaMailSender.send(preparator);
  }
 
}