Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaMail base64 encoding

I have some Java code which sends out an email with code somewhat like the following:

MimeBodyPart part = new MimeBodyPart();
part.setContent(htmlString, "text/html; charset=\"UTF-8\"");
part.setHeader("MIME-Version", "1.0");
part.setHeader("Content-Type", "text/html; charset=\"UTF-8\"");
part.setHeader("Importance", severityVal);

mimeMultiPart.addBodyPart(htmlPart);
mimeMessage.setContent(mimeMultiPart);

... and so on.

How can I encode the "part" MimeBodyPart in base64 for this outgoing email?

like image 639
ragebiswas Avatar asked Dec 27 '22 06:12

ragebiswas


1 Answers

From the docs:

Q: Even though JavaMail does all the encoding and decoding for me, I need to manually control the encoding for some body parts. A: In the rare case that you need to control the encoding, there are several ways to override JavaMail's default behavior. A simple approach is as follows. After creating the entire message, call msg.saveChanges() and then use something like mbp.setHeader("Content-Transfer-Encoding", "base64") to force base64 encoding for the given body part.

Another approach is to subclass MimeBodyPart and override the updateHeaders method so that it first calls super.updateHeaders() and then sets the Content-Transfer-Encoding header as above.

like image 197
dbrin Avatar answered Jan 08 '23 01:01

dbrin