If a base64 encoded image is included in a text/html MimeBodyPart in an img tag, many providers/email clients (including Gmail) simply don't display the image.
So, I've tried splitting the email into parts, but the already base64 encoded image gets munged by an additional encoding process if you put it in an image/jpeg MimeBodyPart.
Instead putting the (same!) encoded data in a PreencodedMimeBodyPart looks like it should be the solution, but then the message cannot be sent:
send message FAILED com.sun.mail.smtp.SMTPSendFailedException: 550 maximum allowed line length is 998 octets, got 73256
Can anybody see what I'm missing or suggest a way to send an inline image from base64 data?
Various attempts (as described) have failed to have the image displayed by Gmail.
Juggling (adding & removing) headers such as .setDisposition("inline"), .setHeader("Content-Transfer-Encoding", "base64") and .addHeader("Content-ID","<image>") also do not help.
I've solved this without using
PreencodedMimeBodyPart. The rare examples of using it on the internet looked either plain wrong or horrible convoluted.What I found to work is to use a
MimeBodyPartand add content to it using a datahandler, decoding the base64 image data as that is done. That way when the automatic encoding is done, it's not acting on data that is already base64 encodedpart2 is the base64 encoding of the image imgType is image/jpeg, img/png etc as appropriate
There needs to be a matching
img[src="cid:image"]tag in the html, where you want the image to appear. This is now tested with a gmail recipient and works as intended (at least there!).