argos/dmp-backend/web/src/main/java/eu/eudat/logic/services/utilities/MailServiceImpl.java

142 lines
4.8 KiB
Java
Raw Normal View History

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;
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;
2020-10-27 11:01:18 +01:00
import javax.mail.BodyPart;
2018-08-30 13:09:36 +02:00
import javax.mail.Message;
import javax.mail.MessagingException;
2020-10-27 11:01:18 +01:00
import javax.mail.internet.MimeBodyPart;
2018-08-30 13:09:36 +02:00
import javax.mail.internet.MimeMessage;
2020-10-27 11:01:18 +01:00
import javax.mail.internet.MimeMultipart;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
2020-10-27 11:01:18 +01:00
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
2018-08-30 13:09:36 +02:00
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 {
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 {
2020-10-27 11:01:18 +01:00
List<String> imageSources = parseImages(mail.getContent());
List<String> cids = new ArrayList<>();
if (!imageSources.isEmpty()) {
for (int i = 0; i < imageSources.size(); i++) {
cids.add(UUID.randomUUID().toString());
}
mail.setContent(replaceImageSources(mail.getContent(), cids));
}
MimeMultipart content = new MimeMultipart("related");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(mail.getContent(), "text/html; charset=UTF-8");
content.addBodyPart(messageBodyPart);
if (!imageSources.isEmpty()) {
for (int i =0; i < imageSources.size(); i++) {
MimeBodyPart imagePart = new MimeBodyPart();
try {
imagePart.attachFile(applicationContext.getResource(imageSources.get(i)).getFile());
imagePart.setContentID("<" + cids.get(i) + ">");
imagePart.setDisposition(MimeBodyPart.INLINE);
content.addBodyPart(imagePart);
} catch (IOException | MessagingException e) {
logger.error(e.getMessage(), e);
}
}
}
2018-08-30 13:09:36 +02:00
MimeMessage message = this.emailSender.createMimeMessage();
2018-01-05 16:40:19 +01:00
message.setSubject(mail.getSubject());
2020-10-27 11:01:18 +01:00
message.setContent(content);
2018-08-30 13:09:36 +02:00
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
public String getMailTemplateContent(String resourceTemplate) {
Resource resource = applicationContext.getResource(resourceTemplate);
2018-08-30 13:09:36 +02:00
try {
InputStream inputStream = resource.getInputStream();
String tempate = new BufferedReader(
new InputStreamReader(inputStream, StandardCharsets.UTF_8)
).lines().collect(Collectors.joining("\n"));
2021-09-27 17:14:21 +02:00
inputStream.close();
return tempate;
2018-08-30 13:09:36 +02:00
} catch (IOException e) {
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");
}
2020-10-27 11:01:18 +01:00
private List<String> parseImages(String content) {
List<String> imagePaths = new ArrayList<>();
int lastIndex = 0;
while (lastIndex != -1) {
lastIndex = content.indexOf("img src=\"", lastIndex);
if (lastIndex != -1) {
imagePaths.add(content.substring(lastIndex + 9, content.indexOf("\"", lastIndex + 9)));
lastIndex ++;
}
}
return imagePaths;
}
private String replaceImageSources(String content, List<String> cids) {
int lastIndex = 0;
int cidIndex = 0;
while (lastIndex != -1) {
lastIndex = content.indexOf("img src=\"", lastIndex);
if (lastIndex != -1) {
content = content.replace(content.substring(lastIndex + 9, content.indexOf("\"", lastIndex + 9)), "cid:" + cids.get(cidIndex));
lastIndex ++;
cidIndex ++;
}
}
return content;
}
2018-01-05 16:40:19 +01:00
}