2018-06-27 12:29:21 +02:00
|
|
|
package eu.eudat.logic.services.utilities;
|
2018-01-05 16:40:19 +01:00
|
|
|
|
2018-06-27 12:29:21 +02:00
|
|
|
import eu.eudat.models.data.mail.SimpleMail;
|
2018-08-30 13:09:36 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2020-01-16 16:46:24 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-01-05 16:40:19 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2018-08-30 13:09:36 +02:00
|
|
|
import org.springframework.context.ApplicationContext;
|
2018-01-08 12:44:48 +01:00
|
|
|
import org.springframework.core.env.Environment;
|
2018-08-30 13:09:36 +02:00
|
|
|
import org.springframework.core.io.Resource;
|
2018-01-05 16:40:19 +01:00
|
|
|
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2018-08-30 13:09:36 +02:00
|
|
|
import javax.mail.Message;
|
|
|
|
import javax.mail.MessagingException;
|
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
import java.io.*;
|
|
|
|
|
2018-02-01 10:08:06 +01:00
|
|
|
|
2018-01-05 16:40:19 +01:00
|
|
|
@Service("mailService")
|
2018-02-16 11:34:02 +01:00
|
|
|
public class MailServiceImpl implements MailService {
|
2020-01-16 16:46:24 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
|
2018-08-30 13:09:36 +02:00
|
|
|
|
2018-01-08 12:44:48 +01:00
|
|
|
private Environment env;
|
2018-01-05 16:40:19 +01:00
|
|
|
|
|
|
|
private JavaMailSender emailSender;
|
|
|
|
|
2018-08-30 13:09:36 +02:00
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public MailServiceImpl(Environment env, JavaMailSender emailSender, ApplicationContext applicationContext) {
|
|
|
|
this.env = env;
|
|
|
|
this.emailSender = emailSender;
|
|
|
|
this.applicationContext = applicationContext;
|
|
|
|
}
|
|
|
|
|
2018-01-05 16:40:19 +01:00
|
|
|
@Override
|
2018-08-30 13:09:36 +02:00
|
|
|
public void sendSimpleMail(SimpleMail mail) throws MessagingException {
|
|
|
|
MimeMessage message = this.emailSender.createMimeMessage();
|
2018-01-05 16:40:19 +01:00
|
|
|
message.setSubject(mail.getSubject());
|
2018-08-30 13:09:36 +02:00
|
|
|
message.setText(mail.getContent(), "utf-8", "html");
|
|
|
|
message.addRecipients(Message.RecipientType.TO, mail.getTo());
|
2018-01-08 12:44:48 +01:00
|
|
|
message.setFrom(env.getProperty("mail.from"));
|
2018-01-05 16:40:19 +01:00
|
|
|
this.emailSender.send(message);
|
|
|
|
}
|
2018-01-08 12:44:48 +01:00
|
|
|
|
|
|
|
public Environment getEnv() {
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-09 13:01:58 +02:00
|
|
|
public String getMailTemplateContent(String resourceTemplate) {
|
|
|
|
Resource resource = applicationContext.getResource(resourceTemplate);
|
2018-08-30 13:09:36 +02:00
|
|
|
try {
|
|
|
|
InputStream inputStream = resource.getInputStream();
|
|
|
|
StringWriter writer = new StringWriter();
|
|
|
|
IOUtils.copy(inputStream, writer, "UTF-8");
|
|
|
|
return writer.toString();
|
|
|
|
} catch (IOException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2018-08-30 13:09:36 +02:00
|
|
|
}
|
|
|
|
return "";
|
2018-01-08 12:44:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getMailTemplateSubject() {
|
|
|
|
return env.getProperty("mail.subject");
|
|
|
|
}
|
2018-01-05 16:40:19 +01:00
|
|
|
}
|