我目前正在尝试使用 Google App Engine 1.9.5 发送一封包含图片的电子邮件。此功能仅在 SDK 1.9.0 版本中可用:
Users now have the ability to embed images in emails via the Content-Id attachment header.
https://code.google.com/p/googleappengine/issues/detail?id=965
https://code.google.com/p/googleappengine/issues/detail?id=10503
来源:https://code.google.com/p/googleappengine/wiki/SdkForJavaReleaseNotes
这是我的代码:
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("leo.mieulet@xxx.com", "xxx.com newsletter"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("leo.mieulet@xx.com", "Leo Mieulet"));
msg.setSubject("Inline image test : "+new Date().getTime());
String imageCid = "graph";
DataSource ds = new ByteArrayDataSource(imageBase64, "image/png");
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.setDataHandler(new DataHandler(ds));
imagePart.setFileName(imageCid + ".png");
imagePart.setHeader("Content-Type", "image/png");
imagePart.addHeader("Content-ID", "<" + imageCid + ">");
String htmlBody = "My html text... <img src=\"cid:"+imageCid+"\"> ... ends here.";
// Create alternate message body.
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<html><body>"+htmlBody+"</body></html>", "text/html");
final Multipart multipart = new MimeMultipart();
multipart.addBodyPart(htmlPart);
multipart.addBodyPart(imagePart);
msg.setContent(multipart);
msg.saveChanges();
Transport.send(msg);
我收到一封电子邮件,如下所示: 谁能帮我解决这个问题吗?
最佳答案
根据 imageBase64 变量名称,您似乎将已经以 Base64 编码的图像提供给 ByteArrayDataSource。您应该直接使用图像字节数组,而不需要 Base64.encode() 它。
关于java - 自 1.9.0 起在 App Engine 上发送包含内嵌图片的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23782628/