how to send HTML email

Don’t upcast your MimeMessage to Message:

MimeMessage simpleMessage = new MimeMessage(mailSession);

Then, when you want to set the message body, either call

simpleMessage.setText(text, "utf-8", "html");

or call

simpleMessage.setContent(text, "text/html; charset=utf-8");

If you’d rather use a charset other than utf-8, substitute it in the appropriate place.

JavaMail has an extra, useless layer of abstraction that often leaves you holding classes like Multipart, Message, and Address, which all have much less functionality than the real subclasses (MimeMultipart, MimeMessage, and InternetAddress) that are actually getting constructed…

Leave a Comment