Java Mail: 邮件中的内联图片未显示

huangapple go评论63阅读模式
英文:

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;">

电子邮件正文:

Java Mail: 邮件中的内联图片未显示

电子邮件正文中的最后一张图片是我设置的pin_image。
但是我在我的电子邮件中没有看到内联图像!我正在使用Thymeleaf模板,在Java代码中设置了contentId pin_image。我做错了什么?

英文:

I am trying to add inline image like this:
Static folder location: resources-> static

MimeMessagePreparator messagePreparator = mimeMessage -&gt; {
                MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
                messageHelper.setTo(recipientList);
                messageHelper.setFrom(fromEmail);
                messageHelper.setSubject(emailInfo.getSubject());
                //ClassPathResource image = new ClassPathResource(&quot;static/pin.png&quot;);
                FileSystemResource image = new FileSystemResource(new File(&quot;static/pin.png&quot;));
    
    
               messageHelper.addInline(&quot;pin_image&quot;,image);
    
                messageHelper.setText(body, true);
    
            };
            sendMail(messagePreparator, emailInfo);

Edit: thymeleaf related part:

    &lt;img src=&quot;cid:pin_image&quot; alt=&quot;location&quot; width=&quot;25&quot; height=&quot;25&quot; border=&quot;0&quot;
style=&quot;display: block; font-family: Arial; color: #666666; font-size: 14px; width: 25px; height: 25px;&quot;&gt;

Email body:

Java Mail: 邮件中的内联图片未显示
<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(&quot;pin_image&quot;,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

huangapple
  • 本文由 发表于 2020年9月7日 21:23:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63778475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定