How to send an HTML email address

I need to send an HTML file by email, but not as an attachment.

Message simpleMessage = new MimeMessage(mailSession); try { fromAddress = new InternetAddress(from); toAddress = new InternetAddress(to); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { simpleMessage.setFrom(fromAddress); simpleMessage.setRecipient(RecipientType.TO, toAddress); simpleMessage.setSubject(subject); simpleMessage.setText(text); Transport.send(simpleMessage); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

It sends emails simply with a text message. I want to send HTML content that is stored in another file, but not as an attachment

+48
html javamail
Mar 07 2018-11-17T00:
source share
1 answer

Do not raise MimeMessage to Message :

 MimeMessage simpleMessage = new MimeMessage(mailSession); 

Then when you want to set the message body, call

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

or challenge

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

If you prefer to use encoding other than utf-8 , replace it in the appropriate place.

JavaMail has an extra, useless abstraction layer that often leaves you with classes like Multipart , Message and Address , which have much less functionality than real subclasses ( MimeMultipart , MimeMessage , and InternetAddress ) that are actually created ...

+99
Mar 07 '11 at 22:29
source share



All Articles