gpt4 book ai didi

java - 电子邮件中的电子邮件内嵌图像不适用于 MacOS 电子邮件

转载 作者:行者123 更新时间:2023-12-01 08:54:43 25 4
gpt4 key购买 nike

我正在使用 spring boot 从 java 发送 HTML 电子邮件。电子邮件包括带有我们公司图像 Logo 的签名。它工作得很好。在 Gmail 上。但在 MacOS 应用程序电子邮件中, Logo 作为附件发送并且未内嵌。

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

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>

最佳答案

看看这个扩展的代码 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>

很方便,不是吗?

关于java - 电子邮件中的电子邮件内嵌图像不适用于 MacOS 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42116738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com