gpt4 book ai didi

java - 使用 Spring 的 JavaMailSender 从 Google App Engine 发送多部分电子邮件失败

转载 作者:行者123 更新时间:2023-12-01 06:21:24 29 4
gpt4 key购买 nike

无需多部分即可正常工作(从 example in Spring documentation 修改):

final MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(final MimeMessage mimeMessage) throws Exception {
final MimeMessageHelper message = new MimeMessageHelper(
mimeMessage);
message.setTo(toAddress);
message.setFrom(fromAddress);
message.setSubject(subject);
final String htmlText = FreeMarkerTemplateUtils
.processTemplateIntoString(configuration
.getTemplate(htmlTemplate), model);
message.setText(htmlText, true);
}
};
mailSender.send(preparator);

但是一旦我将其更改为:

final MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(final MimeMessage mimeMessage) throws Exception {
final MimeMessageHelper message = new MimeMessageHelper(
mimeMessage, true);
...
message.setText(plainText, htmlText);
}
};
mailSender.send(preparator);

我得到:

Failed message 1:
javax.mail.MessagingException: Converting attachment data failed
at com.google.appengine.api.mail.stdimpl.GMTransport.sendMessage(GMTransport.java:231)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:402)
...

由于 GMTransport 是 Google 专有类并且没有可用的源代码,因此很难找出问题(至少以我的技能而言)。有人知道接下来要尝试什么吗?

我的bean配置,用于帮助帮助我:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"
p:username="${mail.username}" p:password="${mail.password}"
p:protocol="gm" />

最佳答案

我最近遇到了这个问题,在任何网站上都找不到解决方案。问题在于 MimeMessageHelper 构造函数默认使用的 multipartMode 参数。

更改:

MimeMessageHelper h = new MimeMessageHelper(mimeMessage, true);

致:

MimeMessageHelper h = new MimeMessageHelper(
mimeMessage,
MimeMessageHelper.MULTIPART_MODE_RELATED);

第一个表单默认为 MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED。文档指出:

This is the default since Spring 1.2.1. This is arguably the most correct MIME structure, according to the MIME spec: It is known to work properly on Outlook, Outlook Express, Yahoo Mail, and Lotus Notes. Does not work properly on Mac Mail. If you target Mac Mail or experience issues with specific mails on Outlook, consider using MULTIPART_MODE_RELATED instead.

它可能应该更新以包含它不能与 AppEngine 的 Mail API 一起使用。替换模式可能不太正确,但似乎更兼容。

关于java - 使用 Spring 的 JavaMailSender 从 Google App Engine 发送多部分电子邮件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2686889/

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