Mix plain text and HTML content in a mail

 Properties props = new Properties();
   props.put("mail.smtp.host", "MYMAILSERVER");
   Session session = Session.getInstance(props,null);
   MimeMessage message = new MimeMessage(session);

   InternetAddress from = new InternetAddress("from@me.com");
   InternetAddress to = new InternetAddress("to@you.com");

   message.setSubject("I am a multipart text/html email" );
   message.setFrom(from);
   message.addRecipient(Message.RecipientType.TO, to);

   Multipart multipart = new MimeMultipart();

   // PLAIN TEXT
   BodyPart messageBodyPart = new MimeBodyPart();
   messageBodyPart.setText("Here is your plain text message");
   multipart.addBodyPart(messageBodyPart);

   // HTML TEXT
   messageBodyPart = new MimeBodyPart();
   String htmlText = "<H1>I am the html part</H1>";
   messageBodyPart.setContent(htmlText, "text/html");
   multipart.addBodyPart(messageBodyPart);

   message.setContent(multipart);
   Transport.send(message);

Done! Happy Coding!

Related posts:

A Guide to Java HashMap
Java Program to Implement Knight’s Tour Problem
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Quick Intro to Spring Cloud Configuration
XML Serialization and Deserialization with Jackson
The HttpMediaTypeNotAcceptableException in Spring MVC
DistinctBy in the Java Stream API
Guide to the Volatile Keyword in Java
Java Program to Represent Graph Using Incidence List
HashMap trong Java hoạt động như thế nào?
Different Ways to Capture Java Heap Dumps
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Find the GCD and LCM of two Numbers
Spring Boot - Introduction
Java Program to Implement Sorted List
Using a Mutex Object in Java
Spring Boot - Servlet Filter
Spring Data Java 8 Support
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java IO vs NIO
Jackson – JsonMappingException (No serializer found for class)
How to Get a Name of a Method Being Executed?
HttpClient Connection Management
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Concurrency Interview Questions and Answers
Object cloning trong java
StringBuilder vs StringBuffer in Java
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Java Program to Implement ArrayBlockingQueue API
Java – Delete a File
Một số ký tự đặc biệt trong Java
Java Program to Describe the Representation of Graph using Adjacency Matrix