【问题标题】:Prevent gmail from showing inline images as attachments防止 gmail 将内联图像显示为附件
【发布时间】:2018-09-24 13:03:20
【问题描述】:

我正在使用spring samples 发送内嵌图像。它可以工作,但 gmail 也将图像显示为附件。如何避免?

代码很简单。

public class Email {

    public static  MimeMessagePreparator getContentAsInlineResourceMessagePreparator(final String to) {

        MimeMessagePreparator preparator = new MimeMessagePreparator() {

            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

                helper.setSubject("Email with inline image");
                helper.setFrom("fake@yourshop.com");
                helper.setTo(to);

                String content = "Dear pedrofb...";
                helper.setText("<html><body><p>" + content + "</p><img src='cid:company-logo'></body></html>", true);
                helper.addInline("company-logo", new ClassPathResource("logo.png"));
            }
        };
        return preparator;
    }
    public final static void main (String argv[]){
        //Basic SMTP configuration
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        mailSender.setPort(port);

        MimeMessagePreparator preparator = getContentAsInlineResourceMessagePreparator("myemail@gmail.com");            
        mailSender.send(preparator);
    }

}

我的问题与How to stop embedded images in email being displayed as attachments by GMail? 类似,但答案很老,并且没有显示如何正确配置弹簧。我不想自己构建消息部分和标题


将原始消息发布到pastebin

【问题讨论】:

  • MimeMessageHelper的第二个参数也带代码docs.spring.io/autorepo/docs/spring/4.1.4.RELEASE/javadoc-api/…。请尝试所有 4 种模式,看看它们中的任何一种是否可以帮助您处理 gmail 电子邮件。另外我建议尝试使用companyLogo 而不是company-logo
  • @TarunLalwani 谢谢。没有运气...
  • 请在 gmail 中查看电子邮件的原始视图并创建一个相同的 pastebin 并分享
  • @TarunLalwani,发布原始消息pastebin.com/uiQMJUHQ

标签: java spring email gmail


【解决方案1】:

问题在于MimeType 的决心

png 扩展名被视为image/x-png 而不是image/png 这会导致 Gmail 出现问题。这已在5.X 中修复/更改,也可能在某些4.X 更高版本中(我不确定这些)。但修复很容易。改变

MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8") {

    @Override
    public void addInline(String contentId, Resource resource) throws MessagingException {
        Assert.notNull(resource, "Resource must not be null");
        String contentType = this.getFileTypeMap().getContentType(resource.getFilename());
        contentType = contentType.replace("x-png", "png");
        this.addInline(contentId, resource, contentType);

    }
};

它会将MimeType 覆盖为image/png

【讨论】:

    【解决方案2】:

    我已经执行了你的代码。它运行良好:我在 gmail 中打开生成的电子邮件,我看到了内联图像,但我没有看到任何附件! 这可能是由于库版本?我用的是 5.0.5.RELEASE。

    如果这不是解决方案,我猜您的 SmtpServer.toJavaMailSender() 或 Gmail 设置中可能有一些不常见的属性。

    【讨论】:

    • 我使用的是 spring 4.1.9 和 javamail 1.4.7。我正在使用一个非常简单的 SMTP 配置
    猜你喜欢
    • 2023-01-23
    • 2019-03-14
    • 2015-04-16
    • 2012-01-11
    • 1970-01-01
    • 2020-05-29
    • 2021-12-19
    • 2011-10-30
    • 2011-05-01
    相关资源
    最近更新 更多