嘿,我正在尝试将图像嵌入我的电子邮件正文,该图像将在 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;
}
上面的代码会生成一封带有嵌入式图像和正文消息的电子邮件:
下面的代码是我为体内的多于一张嵌入式图像想到的:
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 + "\" />");
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以将图像直接插入HTML
messageBodyPart.setText(""
+ "<html>"
+ " <body>"
+ " <p>Here is my image:</p>"
+ " <img src=\"data:image/jpg;base64," + Base64.getEncoder().encodeToString(YOUR_IMAGE_DATA) + "\" />"
+ " </body>"
+ "</html>"
,"US-ASCII", "html");
multipart.addBodyPart(messageBodyPart);
如果不使用JPEG,请更改图像的模仿类型。