'Embeded image CID issue in SimpleJavaMail

I write a Java 8 code that sends e-mails using SimpleJavaMail ( https://www.simplejavamail.org ) and I am using an HTML content that has embedded images e.g. <img src="cid:ji">

I add the image via currentEmailBuilder.withEmbeddedImage("ji" , new FileDataSource("smiley.png"))

After that, the e-mail is delivered but the image is not inline, instead comes as an attachment.

This is because FileDataSource does not understand the Mime Type of the png file (although it is a proper png file) and reports it by default as application/octet-stream.

Furthermore when the attached image is e.g. JPG , then the mime type is properly detected but it is attached with an unexpected CID. Instead of the one that I use in the template (e.g. ji) it comes with the file extension along (!!) i.e. ji.jpg and thus the CID does not match the template so the image again does not appear properly.

I must be doing something really wrong!

I employed a workaround by creating a class of my own named ExtendedFileDataSource that reports correct MimeType and CID but it is still very strange that I have searched a lot and nobody seems to have this or a similar problem with SimpleJavaMail.

Can you help me out so that I can get rid of the extra class?

Thank you in advance for your time!!

Panagiotis


In case you need to see the code that embeds the images is:

for (Map.Entry<String, String> entry : images.entrySet()) {
    // I use a TreeMap that maps the file path to the CID (name)
    final String path = entry.getKey();
    final String name = entry.getValue();

    // this call created the file from a custom service that I created to handle the filesyste
    final File file = FilesService.api.fileFromDbNPath(account.getDatabase(), path);
    if (!file.exists()) {
        throw new ApiException(ApiErrorCodes.MailInexistentImage, "database", account.getDatabase(), "path", path);
    }
    mailBuilder.withEmbeddedImage(name, new FileDataSource(file));
}

and the resulting e-mail has these two parts (among others):

------=_Part_2_1209984735.1634128409176
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<p><b>Na =CF=84=CE=BF=CF=82 =CE=BF =CE=B1=CF=81=CF=87=CE=B7=CE=B3=CF=8C=CF=
=82:</b> =CE=9B=CE=BF=CF=8D=CE=BB=CE=B7=CF=82 =CE=A0=CE=B9=CE=BB=CE=BF=CF=
=8D=CE=BB=CE=B7=CF=82 =CE=BC=CE=B5 =CF=84o =CE=BC=CE=AD=CE=B9=CE=BB!</p><p>=
=CE=96=CE=97=CE=A4=CE=A9!!</p>
<hr>
<p><img src=3D"cid:efydpepxon"><br><img src=3D"cid:ji"><br><img src=3D"cid:=
testimage"></p>
<hr>
<h1>Fin</h1>
------=_Part_2_1209984735.1634128409176--
------=_Part_1_380493606.1634128409176
Content-Type: application/octet-stream; filename=testimage.png; name=testimage.png
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=testimage.png
Content-ID: <testimage.png>

iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAMAAABrrFhUAAAC/VBMVEUXFBAiHRYrJRxOMQMzLihV
OQIOhRhEOi1eQ...............................7IwbTAwASAIPHaQDihBgQ4fUhBRQdCOx
HPaGyXeFOSEfP0LX3UwUgE+ZfgmslckQ8LdK3ik6zMM/0KX1yGoJ++AlMbAfc4Oe7wEA2lSmadFe
khgCOHdJbIL/UGaT97paY2E/5gb/H72WgFosmUw1AAAAAElFTkSuQmCC
------=_Part_1_380493606.1634128409176
Content-Type: image/jpeg; filename=ji.jpg; name=ji.jpg
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=ji.jpg
Content-ID: <ji.jpg>

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAUEBAQEAwUEBAQGBQUGCA0ICAcHCBALDAkNExAUExIQ
EhIUFx0ZFBYcFhISGiMaHB4fISEhFBkkJyQgJh0gISD/2wBDAQUGBggHCA8ICA8gFRIVICAgICAg
ICAgICAgICAgI...........................NFFABmjNFFABmjNFFABmjNFFABmjNFFABmjN
FFABmjNFFABmjNFFABmjNFFABmjNFFABmjNFFABmjNFFABmjNFFABmjNFFABmjNFFADGVmORIy+w
x/UUUUUAf//Z
------=_Part_1_380493606.1634128409176


Solution 1:[1]

Your mail source code show you the id Content-ID: <testimage.png> & Content-ID: <ji.jpg>. That means the id in your html file/code img has to be the same --> <img src"cid:testimage.png"/> & <img src"cid:ji.jpg"/>.

I had exactly the same issue, and never find an answer until I do it myself, so I hope my answer will help someone.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Liberta