【问题标题】:Email inline image in email not working on MacOS email电子邮件中的电子邮件内联图像不适用于 MacOS 电子邮件
【发布时间】:2017-06-26 06:49:32
【问题描述】:

我正在使用 Spring Boot 从 Java 发送 HTML 电子邮件。电子邮件包括带有我们公司图像标志的签名。它工作得很好。在 gmail 上。但在 MacOS 应用程序电子邮件中,徽标是作为附件发送的,而不是内联的。

代码中不相关的部分被替换为 ...

final Locale locale = ...;
final MimeMessage mail = javaMailSender.createMimeMessage();
MimeMessageHelper helper;
try {
    helper = new MimeMessageHelper(mail, true, "UTF-8");
    helper.setTo(...);
    helper.setCc(...);
    helper.setBcc(...);
    helper.setFrom(...);

    final String htmlContent = templateEngine.process(..., new Context(locale, ...));
    helper.setText(htmlContent, true);
    helper.addInline("myImg", new ClassPathResource(".../myImg.png"));
} catch (UnsupportedEncodingException | MessagingException e) {
    throw new MailSendException(..., e);
}
javaMailSender.send(mail);

HTML 由 thymeleaf 生成,如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
...
<img src=".../myImg.png" th:src="'cid:' + ${'myImg'}" />

</body>
</html>

【问题讨论】:

    标签: java image email spring-boot


    【解决方案1】:

    看看这个扩展程序Spring Boot Email Tools的代码,也许你能找到更好的方法来创建电子邮件。

    我个人认为,如果您直接使用它通过EmailService bean 发送电子邮件,可能会节省一些时间。来自文档(示例基于 Freemarker 模板引擎):

    @Autowired
    public EmailService emailService;
    
    public void sendEmailWithTemplating(){
       Arrays.asList(new Cospirator("cassius@sic-semper.tyrannis", "Gaius Cassius Longinus"),
                new Cospirator("brutus@sic-semper.tyrannis", "Marcus Iunius Brutus Caepio"))
            .stream.forEach(tyrannicida -> {
           final Email email = DefaultEmail.builder()
                .from(new InternetAddress("divus.iulius@mala-tempora.currunt", "Gaius Iulius Caesar"))
                .to(Lists.newArrayList(new InternetAddress(tyrannicida.getEmail(), tyrannicida.getName())))
                .subject("Idus Martii")
                .body("")//Empty body
                .encoding(Charset.forName("UTF-8")).build();
            //Defining the model object for the given Freemarker template
            final Map<String, Object> modelObject = new HashMap<>();
            modelObject.put("tyrannicida", tyrannicida.getName());
    
           emailService.send(email, "idus_martii.ftl", modelObject);
       };
    }
    
    private static class Cospirator {
      private String email;
      private String name;
      public Cospirator(final String email, final String name){
        this.email = email;
        this.name = name;
      }
    
      //getters
    }
    

    你需要的依赖是

    <dependency>
        <groupId>it.ozimov</groupId>
        <artifactId>spring-boot-email-core</artifactId>
        <version>0.4.0</version>
    </dependency>
    <dependency>
        <groupId>it.ozimov</groupId>
        <artifactId>spring-boot-thymeleaf-email</artifactId>
        <version>0.4.0</version>
    </dependency>
    

    很方便,不是吗?

    【讨论】:

    • 谢谢,我会调查的。不要认为它会为我节省任何编码 atm,但如果它有效......
    • 好吧,所以我试过了,但没有成功。以 org.springframework.beans.factory.BeanCreationException 结束:创建名为“redisKeyValueAdapter”的 bean 时出错。我的意思是......我可能会在一段时间后完成这项工作,但我宁愿使用标准的弹簧方式,使用我发布的代码(它可能只是一些小的补充)。我看不到 Spring Boot 电子邮件工具的附加价值。代码并不是真的“更整洁”:/
    • 我认为你做了一个错误的设置,你似乎正在使用持久层,根据文档这是可选的。您应该设置以下属性:spring.mail.persistence.enabled: false spring.mail.persistence.redis.embedded: false spring.mail.persistence.redis.enabled: false。无论如何,我认为如何解决这个问题完全取决于你
    猜你喜欢
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 2011-03-03
    • 1970-01-01
    相关资源
    最近更新 更多