英文:
Java Mail: Inline Image is not shown in email
问题
我正在尝试添加内联图像,类似于以下方式:
静态文件夹位置:resources-> static
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
messageHelper.setTo(recipientList);
messageHelper.setFrom(fromEmail);
messageHelper.setSubject(emailInfo.getSubject());
// ClassPathResource image = new ClassPathResource("static/pin.png");
FileSystemResource image = new FileSystemResource(new File("static/pin.png"));
messageHelper.addInline("pin_image", image);
messageHelper.setText(body, true);
};
sendMail(messagePreparator, emailInfo);
编辑:与Thymeleaf相关的部分:
<img src="cid:pin_image" alt="location" width="25" height="25" border="0" style="display: block; font-family: Arial; color: #666666; font-size: 14px; width: 25px; height: 25px;">
电子邮件正文:
电子邮件正文中的最后一张图片是我设置的pin_image。
但是我在我的电子邮件中没有看到内联图像!我正在使用Thymeleaf模板,在Java代码中设置了contentId pin_image
。我做错了什么?
英文:
I am trying to add inline image like this:
Static folder location: resources-> static
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
messageHelper.setTo(recipientList);
messageHelper.setFrom(fromEmail);
messageHelper.setSubject(emailInfo.getSubject());
//ClassPathResource image = new ClassPathResource("static/pin.png");
FileSystemResource image = new FileSystemResource(new File("static/pin.png"));
messageHelper.addInline("pin_image",image);
messageHelper.setText(body, true);
};
sendMail(messagePreparator, emailInfo);
Edit: thymeleaf related part:
<img src="cid:pin_image" alt="location" width="25" height="25" border="0"
style="display: block; font-family: Arial; color: #666666; font-size: 14px; width: 25px; height: 25px;">
Email body:
<br>The last image in the email-body is where I set pin_image.
But I get no inline image in my email! I am using using thymeleaf template where I set contentId pin_image
as in java code. What am I doing wrong?
答案1
得分: 2
我只是通过改变这两行的顺序来解决了:
messageHelper.addInline("pin_image",image);
messageHelper.setText(body, true);
我认为,我在 HTML 尚未提供的情况下尝试创建 contentId。这就是为什么会遇到这个问题。
附注:我的整个 HTML 内容都在 body
变量中,这是我的模板。
英文:
I solved just altering the orders of these two lines:
messageHelper.addInline("pin_image",image);
messageHelper.setText(body, true);
I think, I was trying to create an contentId when the HTML is not yet provided. That's why was facing the issue.
P.S: My whole HTML was in the body
variable which is my template
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论