Я пытаюсь добавить встроенное изображение в шаблон электронной почты html, как в уроках, но ничего не работает.
Это мой метод из класса обслуживания:
public void sendOrderDetailsEmail(PortfolioModel model) {
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
Context context = new Context();
context.setVariable("portfolio", model);
context.setVariable("products", model.getProducts());
context.setVariable("logo", "logo");
String htmlContent = templateEngine.process(ORDER_DETAILS_HTML, context);
message.addInline("logo", new ClassPathResource("images/logo.png"), "image/png");
LOG.info("Sending order configuration email to " + model.getUser().email);
configureMessage(message, model.getUser().email, "Order details", htmlContent);
mailSender.send(mimeMessage);
} catch (Exception e) {
LOG.error(e);
}
}
private void configureMessage(MimeMessageHelper message, String to, String subject, String content)
{
message.setFrom(new InternetAddress("myemail@address.org", "info"));
message.setTo(to);
message.setSubject(subject);
message.setText(content, true /* is html */);
}
А это мой шаблон из тимьяна HTML:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://thymeleaf.org">
<head>
</head>
<body>
<p>
<img src="logo.png" th:src="|cid:${logo}|" />
</p>
<div th:object="${portfolio}">
<p><span th:utext="${portfolio.user.fullname}"></span></p>
<p><b>Your order id: </b><span th:utext="${portfolio.orderId}"></span></p>
<p><b>Order information:</b></p>
<div th:if="${products} != null">
<table>
<tr th:each="product : ${products}">
- Product:
<td th:text="${product.model}"></td >
<td th:text="${product.year}"></td >
<td th:text="${product.color}"></td >
</tr>
</table>
</div>
</div>
</body>
</html>
Все выше
<img src=...>
, который не появляется, работает отлично.