Fix emails that are sent with images

This commit is contained in:
George Kalampokis 2020-10-27 12:01:18 +02:00
parent c8a163f4aa
commit 2b89f646ee
2 changed files with 68 additions and 2 deletions

View File

@ -11,10 +11,16 @@ import org.springframework.core.io.Resource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Service("mailService")
@ -36,9 +42,34 @@ public class MailServiceImpl implements MailService {
@Override
public void sendSimpleMail(SimpleMail mail) throws MessagingException {
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);
}
}
}
MimeMessage message = this.emailSender.createMimeMessage();
message.setSubject(mail.getSubject());
message.setText(mail.getContent(), "utf-8", "html");
message.setContent(content);
message.addRecipients(Message.RecipientType.TO, mail.getTo());
message.setFrom(env.getProperty("mail.from"));
this.emailSender.send(message);
@ -66,4 +97,39 @@ public class MailServiceImpl implements MailService {
public String getMailTemplateSubject() {
return env.getProperty("mail.subject");
}
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;
}
}

View File

@ -260,7 +260,7 @@
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<img src=".\images\OpenDMP.png" alt="OpenDMP" width="100" height="81">
<img src="classpath:images\OpenDMP.png" alt="OpenDMP" width="100" height="81">
<h2>Thank you for joining OpenDMP!</h2>
<p>Please confirm that your email address is correct to continue.
<br/>The link will expire in {expiration_time}.</p>