Java 如何使用ThymeLeaf发送带有内联图像的电子邮件
我试图用ThymeLeaf和Spring发送一封带有内联图像的电子邮件,但迄今为止没有成功。电子邮件会发送,但内联图像不会显示在电子邮件中 该项目不是基于网络的(不是一个网站),而是一个独立的桌面,不是移动的 以下是获取图像文件的方式:Java 如何使用ThymeLeaf发送带有内联图像的电子邮件,java,thymeleaf,Java,Thymeleaf,我试图用ThymeLeaf和Spring发送一封带有内联图像的电子邮件,但迄今为止没有成功。电子邮件会发送,但内联图像不会显示在电子邮件中 该项目不是基于网络的(不是一个网站),而是一个独立的桌面,不是移动的 以下是获取图像文件的方式: URL url = getClass().getResource("/LawFirmAdvisoryGroup.jpg"); File file = new File(url.getPath()); MultipartFile multipartFile =
URL url = getClass().getResource("/LawFirmAdvisoryGroup.jpg");
File file = new File(url.getPath());
MultipartFile multipartFile = new MockMultipartFile(file.getName(),
file.getName(), "image/jpeg", IOUtils.toByteArray(input));
我的服务级别:
@Autowired
private JavaMailSender mailSender;
@Autowired
private TemplateEngine templateEngine;
public void sendMailWithInline(final String recipientName, final String recipientEmail, final MultipartFile image, final byte[] imageBytes)
throws MessagingException {
final Context ctx = new Context();
ctx.setVariable("imageResourceName", image.getName()); // so that we can reference it from HTML
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message
= new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setSubject("Inline Image");
message.setFrom("XXXX@yahoo.com");
message.setTo(recipientEmail);
// Add the inline image, referenced from the HTML code as "cid:${imageResourceName}"
final InputStreamSource imageSource = new ByteArrayResource(imageBytes);
message.addInline(image.getName(), imageSource, image.getContentType());
final String htmlContent = this.templateEngine.process("left_sidebar.html", ctx);
message.setText(htmlContent, true);
this.mailSender.send(mimeMessage);
}
HTML:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:remove="all">Email with inline image</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>
<img src="LawFirmAdvisoryGroup.jpg" th:src="'cid:' + ${imageResourceName}" />
</p>
</body>
</html>
带有内联图像的电子邮件
只需将调用移动到setText()
上几行即可
javadoc for说:
注意:在之后调用addInline
;否则,邮件阅读器可能无法正确解析内联引用
这非常有效:
只需添加一个指向托管在远离桌面的外部服务器上的图像的链接。使用内联CSS,而不是CSS类
此网站将帮助您将CSS类转换为内联CSS
避免任何花哨的CSS,只需使用最基本的CSS。如果您希望HTML邮件能够轻松地流动,即使是在移动设备和其他较小的屏幕中,也应该尽可能避免浮动(比如float:left;)
包括在项目库中,并将spring.xml更改为:
<!-- THYMELEAF: Template Resolver for email templates -->
<bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
<property name="prefix" value="resources/WEB_INF/HTMLMailTemplates/XXXX/html/" />
<!-- <property name="templateMode" value="HTML5" /> -->
<property name="templateMode" value="LEGACYHTML5" />
<property name="characterEncoding" value="UTF-8" />
<property name="order" value="1" />
</bean>
“到目前为止还没有成功”你真的应该试着对正在发生的事情进行更多的描述,而不是仅仅说“它不起作用”。电子邮件已发送,但内联图像不会显示在电子邮件中。@Program Me Rev,您解决了吗?@Program Me Rev提供的以下解决方案是否解决了此问题?我也面临同样的问题。@Programme-Me Rev我可以知道什么是输入吗?你能详细说明一下吗?我也有同样的问题?我创建了一个实用程序类,在构建电子邮件之前我设置了内联图像,内联图像不起作用,我移动到了最后,神奇地工作着,非常感谢。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email With Inline Images</title>
<style>
body {
background-image: url('https://dl.dropbox.com/s/XXXX/pageBackGround.gif');
background-repeat: repeat;
margin:0;
outline:0;
}
.pageContentWrapper {
padding:10px;
width: 100%;
background-image: url('https://dl.dropbox.com/s/XXXX/smallerInlineImage.gif');
background-repeat: repeat;
}
.smallerInlineImage {
width: 22px;
height: 22px;
padding: 0 4px 6px 0;
float: left;
}
</style>
</head>
<body>
<div class="pageContentWrapper">
<div class="smallerInlineImage">
<img src="https://dl.dropboxusercontent.com/s/3ydel6zp53pb65b/smallerInlineImage.png" height="22" width="22">
</div>
</div>
</body>
@Service
public class ThymeEmailService {
@Autowired
private JavaMailSender mailSender;
@Autowired
private TemplateEngine templateEngine;
public void sendMailWithInline() throws MessagingException {
final Context ctx = new Context();
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setSubject("Sample Email Subject");
message.setFrom("senderEmail@yahoo.com");
message.setTo("recipientEmail@yahoo.com");
final String htmlContent = this.templateEngine.process("emailTemplate.html", ctx);
message.setText(htmlContent, true);
String[] attachments = {"C:\\Users\\MyPc\\Dropbox\\CV\\myPDFAttachment.pdf"};
for (String attachment : attachments) {
FileSystemResource file = new FileSystemResource(attachment);
message.addAttachment(file.getFilename(), file);
}
this.mailSender.send(mimeMessage);
}
}