Java Mail larger base64 images have newlines in Linux causing them to fail in clients like Apple's Mail.app

StackOverflow https://stackoverflow.com/questions/22903232

Question

I am creating an email that includes an embedded QR code using base64. One of the features is for this QR code to have a small logo overlaid in the center of the code. Here are the 2 resulting images:

QR codes

The image with the logo is around 3kb and the plain one is about 400 bytes.

I embedded both of them in an email using the following code:

//I pre-encoded the QR codes into a Base64 string first WITHOUT any linebreaks.
PreencodedMimeBodyPart base64Body = new PreencodedMimeBodyPart("base64");
base64Body.setHeader("Content-ID", "<" + key + ">");
base64Body.setHeader("Content-Location", "image.png");
base64Body.setHeader("Content-Type", "image/png; name=\"image.png\"");
base64Body.setDisposition(MimeBodyPart.INLINE);
base64Body.setFileName("image.png");
base64Body.setContent(theEncodedBase64ImageString, "image/png");

This actually works fine when I use my development machine (Mac), however, when I deploy it to a Linux server env't, I find that line breaks seem to be surreptitiously added to the base64 string causing it to display improperly in Apple's Mail.app. When viewed in gmail, the same email displays the QR code properly. Images here:

in different email clients

NOTE: These are the same image (with the GV logo) displayed on GMAIL (left) and Mail.app (right).

I checked the original message contents using gmail and found out that line breaks had been added to my base64 string!

enter image description here

These line breaks only occurred on the Linux server and not my development mac. Also, when I generated the QR code without a logo (400 bytes), I did not encounter this error at all.

I'm not sure which factors are relevant and are causing this issue.

  • Is it the OS?
  • Or perhaps are the mail servers I am using affecting this somehow (I use smtp.gmail.com on my mac and a network mail server on the Linux server)?
  • Is there a file size limit/length on base64-encoded strings in Linux?

Any point in the right direction would be appreciated.

Was it helpful?

Solution 2

The main issue is that apparently, the mail server I am using enforces a per-line maximum no. of characters. If a line exceeds this limit, the server will insert a line-break that apparently contains something extra that some mail applications cannot read.

The fix is to do the line breaks myself manually, so that I am in control of what I'm actually inserting in the base64 string.

In the end, the base64 string with line breaks was properly decoded my the email clients I tested it on. Thanks @Bill Shannon for the unlikely push into this area.

OTHER TIPS

Line breaks shouldn't make any difference as long as the data is correct.

But, you might be running into a JavaMail bug if you're using an old version. What version of JavaMail are you using on each machine?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top