Hey all I am trying to embed an image into my email body that will be viewed in Outlook 2016.
The problem I am coming across is if I want more than one image embedded into the body of the message then how would I do this?
I am currently creating a body message that looks like this:
< h1>I have some attachments for you. %img< /h1>
Which the %img is replaced one every loop with the image cid name ("< img src=\"cid:" + cid + "\" />")
The current code that works but with only 1 embedded image:
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;
}
The code above produces an email with the embedded image and body message:
And this code below is what I came up with for more than just 1 embedded image in the body:
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);
}
}
This does produce an email but it looks like this:
So nothing in the body at all but it did have the embedded image (2.gif)
I can not seem to graps what I am missing in order for it to work as I intended for it too.
I'm probably over-thinking this but help would be appreciated!
UPDATE
Replacing the UUID with the default random sequence produces this:
Reverent code changed:
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 + "\" />");