-已解决 Springboot发送邮件内容包含图片却无法显示 helper.addInline();

Springboot发送邮件内容包含图片却无法显示

解决连接: https://www.manongdao.com/article-1736914.html

  • 错误大致内容
    // 图片
    ClassPathResource cpr = new ClassPathResource("assets/images/激活码.png");
    helper.addInline("p1", cpr.getFile());
    
    // html模板
    String aTag = MessageFormat.format(ACTIVE_URL, IP, "uid=" + uid, "code=" + activeCode);
    helper.setText(this.htmlTemplate(account, DESC + activeCode + aTag), allowHtml);
  • 原因: MimeMessageHelper 的执行顺序错了,先执行 setText() 然后执行 addInline() 添加图片
Inline resources are added to the MimeMessage by using the specified Content-ID (identifier1234 in the above example).   
The order in which you add the text and the resource are very important.   
Be sure to first add the text and then the resources.  
 If you are doing it the other way around, it does not work.
通过使用指定的Content-ID(上面示例中的identifier1234)将内联资源添加到MimeMessage。

添加文本和资源的顺序非常重要。

一定要先添加文本,然后添加资源。

如果你用另一种方式来做,它不会起作用。
  • 所以调换顺序
    // html模板
    String aTag = MessageFormat.format(ACTIVE_URL, IP, "uid=" + uid, "code=" + activeCode);
    helper.setText(this.htmlTemplate(account, DESC + activeCode + aTag), allowHtml);
    
    // 图片
    ClassPathResource cpr = new ClassPathResource("assets/images/激活码.png");
    helper.addInline("p1", cpr.getFile());

在这里插入图片描述

  • 有些邮箱认为你的图片有病毒, 需要用户自己确认信任该图片。
    在这里插入图片描述
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值