fr.lepellerin.ecole.service.internal.EmailServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for fr.lepellerin.ecole.service.internal.EmailServiceImpl.java

Source

/*
   Copyright (C)  Seun Landsberg 2016
    
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
    
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

package fr.lepellerin.ecole.service.internal;

import fr.lepellerin.ecole.service.EmailService;

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.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.util.Arrays;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private TemplateEngine templateEngine;

    @Override
    public void sendSimpleMail(final String sujet, final String to, final String from, final String templateName,
            final Context mailContext) throws MessagingException {
        this.sendSimpleMail(sujet, Arrays.asList(to), from, templateName, mailContext);
    }

    @Override
    public void sendSimpleMail(final String sujet, final List<String> to, final String from,
            final String templateName, final Context mailContext) throws MessagingException {
        final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
        final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
        message.setSubject(sujet);
        message.setFrom(from);
        message.setTo(to.toArray(new String[] {}));

        final String htmlContent = this.templateEngine.process(templateName, mailContext);
        message.setText(htmlContent, true);

        this.mailSender.send(mimeMessage);

    }

}