SMTPSendMailTask.java

package com.sintia.ffl.admin.optique.catalogue.util.mail;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Profile;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
@Profile("smtp")
@RequiredArgsConstructor
public class SMTPSendMailTask implements SendMailTask {

    public final JavaMailSender emailSender;

    /**
     * Sending mail .<br>
     *
     * @param recipients   - An array of the mails that will receive the mail
     * @param cc           - An array of the mails that will receive the mail in
     *                     copy
     * @param subject      - the subject of the mail
     * @param body         - A pre-formatted data that will construct the body of
     *                     the message
     * @param sendMailFrom - the Mail of the sender of the mail
     */
    public void sendMail(String[] recipients, String[] cc, String subject, String body, String sendMailFrom) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();

        mailMessage.setTo(recipients);
        if (cc != null) {
            mailMessage.setCc(cc);
        }
        mailMessage.setSubject(subject);
        mailMessage.setText(body);
        mailMessage.setFrom(sendMailFrom);
        emailSender.send(mailMessage);
    }

}