Display Base64 encoded images in email using Java

Mumtaz
1 min readJan 19, 2020

--

Let’s say that you have an HTML template with <img> tags. Normally to display a base64 encoded image in a browser you would do

<img src=”data:image/png;base64, base64EncodedString" />

But this approach won’t work for all mail clients. For instance Gmail blocks display of all base64 encoded strings for security and spamming reasons. By providing the image with a Content-ID (prefixing it with cid:) and attaching it to the mail and later referencing this image in the body of the message, this issue can be solved.

This is how we can achieve this, by adding an Image part to a multi part message:

And in your html template you should have

<img src="cid:qrImage" alt="qr code">

That’s it, now Gmail and other mail clients should display all the inline images.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Mumtaz
Mumtaz

Written by Mumtaz

Software Developer, India

Responses (3)

Write a response