【发布时间】:2019-02-07 15:04:03
【问题描述】:
大家好,我正在尝试将图像嵌入到我的电子邮件正文中,该图像将在 Outlook 2016 中查看。
我遇到的问题是,如果我希望在邮件正文中嵌入多个图像,那么我该怎么做呢?
我目前正在创建如下所示的正文消息:
我有一些附件给你。 %img
其中 %img 被 每个循环 替换为 图像 cid 名称 ("")
当前有效的代码,但只有 1 个嵌入图像:
private static boolean createEmbeddedImg(MimeBodyPart messageBodyPart, Multipart multipart) throws MessagingException, IOException {
int seq = 0;
int c = (seq++) % 100000;
String cid = c + "." + System.currentTimeMillis();
messageBodyPart.setText(""
+ "<html>"
+ " <body>"
+ " <p>Here is my image:</p>"
+ " <img src=\"cid:" + cid + "\" />"
+ " </body>"
+ "</html>"
,"US-ASCII", "html");
multipart.addBodyPart(messageBodyPart);
MimeBodyPart imagePart = new MimeBodyPart();
try {
byte[] decodedImg = Base64.getDecoder().decode(B64.getBytes(StandardCharsets.UTF_8));
Path destinationFile = Paths.get("c:/temp", "homer.gif");
Files.write(destinationFile, decodedImg);
} catch (IOException e) {
e.printStackTrace();
return false;
}
imagePart.attachFile("c:/temp/homer.gif");
imagePart.setContentID("<" + cid + ">");
imagePart.setDisposition(MimeBodyPart.INLINE);
multipart.addBodyPart(imagePart);
multipart.addBodyPart(messageBodyPart);
return true;
}
上面的代码会生成一封带有嵌入图像和正文的电子邮件:
下面的代码是我想出的不仅仅是 1 个嵌入图像在正文中:
private static void _createEmbeddedImgs(MimeBodyPart messageBodyPart, Multipart multipart, String message,
String[] embeddedImgs) throws MessagingException, IOException {
UUID uuid = UUID.randomUUID();
String cid = null;
List<String> savedCIDS = new ArrayList<String>();
if (embeddedImgs != null && embeddedImgs.length > 0) {
for (String filePath : embeddedImgs) {
cid = String.valueOf(uuid.variant());
message = message.replaceFirst("%img", "<img src=\"cid:" + cid + "\" />");
MimeBodyPart imagePart = new MimeBodyPart();
byte[] decodedImg = Base64.getDecoder().decode(B64.getBytes(StandardCharsets.UTF_8));
Path destinationFile = Paths.get("c:/temp", cid + ".gif");
try {
Files.write(destinationFile, decodedImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imagePart.attachFile("c:/temp/" + cid + ".gif");
imagePart.setContentID("<" + cid + ">");
imagePart.setDisposition(MimeBodyPart.INLINE);
multipart.addBodyPart(imagePart);
savedCIDS.add(String.valueOf(cid));
}
messageBodyPart.setText("<html><body>" + message + "</html></body>", "US-ASCII", "html");
multipart.addBodyPart(messageBodyPart);
}
}
这确实会产生一封电子邮件,但看起来像这样:
所以正文中什么都没有,但它确实有嵌入的图像 (2.gif)
我似乎无法掌握我所缺少的东西,以便它也能按我的预期工作。
我可能想多了,但我们将不胜感激!
更新
将 UUID 替换为默认随机序列会产生以下结果:
尊敬的代码已更改:
int seq = 0;
int c = (seq++) % 100000;
String cid = c + "." + System.currentTimeMillis();
if (embeddedImgs != null && embeddedImgs.length > 0) {
for (String filePath : embeddedImgs) {
message = message.replaceFirst("%img", "<img src=\"cid:" + cid + "\" />");
【问题讨论】:
-
这只是一个猜测,但这可能是
cid包含无效字符的问题吗?在您的单图像代码中,您有数字和点,而在多图像代码中,您使用 uuid,其中包含字符和减号(这可能是问题所在)。 -
顺便说一句,
String.valueOf(cid)是多余的,因为cid已经是一个字符串。 -
@Thomas 我更新了我的 OP 以显示您的建议,但它似乎也不起作用。
-
另一个问题可能是正确解码图像。您是否验证了 gif 是否有效?您发布的代码表明
filePath包含图像文件的 base64 编码内容,但您不使用它,而是使用byte[] decodedImg = Base64.getDecoder().decode(B64.getBytes(StandardCharsets.UTF_8));。
标签: java image email outlook jakarta-mail