Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send an HTML email?

People also ask

Can you send HTML files via email?

The most important thing to know about HTML email is that you can't just attach an HTML file and a bunch of images to a message, then click send. Most of the time, your recipient's email application will break all the paths to your image files by moving your images into temporary folders on the recipient's hard drive.

Can I send HTML email through Gmail?

You can send HTML email in Gmail. But, in the past you would have to design the email, get someone to build it, and then manipulate Gmail code to add it. Now you can create and send HTML email in Gmail by dragging, dropping and a click.


As per the Javadoc, the MimeMessage#setText() sets a default mime type of text/plain, while you need text/html. Rather use MimeMessage#setContent() instead.

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

For additional details, see:

  • GMail Media Queries
  • GMail CSS Design
  • CSS support in mail clients

Set content type. Look at this method.

message.setContent("<h1>Hello</h1>", "text/html");

If you are using Google app engine/Java, then use the following...

MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(SENDER_EMAIL_ADDRESS, "Admin"));
msg.addRecipient(Message.RecipientType.TO,
                 new InternetAddress(toAddress, "user");

msg.setSubject(subject,"UTF-8");

Multipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(message, "text/html");
mp.addBodyPart(htmlPart);
msg.setContent(mp);
Transport.send(msg);

Since JavaMail version 1.4, there is an overload of setText method that accepts the subtype.

// Passing null for second argument in order for the method to determine
// the actual charset on-the fly.
// If you know the charset, pass it. "utf-8" should be fine
msg.setText( message, null, "html" );