【发布时间】: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