阻止 gmail 将内联图像显示为附件
我正在使用弹簧样品发送内联图像。它可以工作,但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);
}
}
我的问题类似于如何阻止电子邮件中的嵌入式图像被GMail显示为附件?但答案非常陈旧,它没有显示如何正确配置弹簧。我不想自己构建消息部分和标头
在 pastebin 中发布了原始消息