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:
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Java Program to Implement Bellman-Ford Algorithm
Java Program to Implement Queue
Java Program to Perform Searching Using Self-Organizing Lists
Java Program to Implement Heap
Finding the Differences Between Two Lists in Java
Upload and Display Excel Files with Spring MVC
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Java Program to Implement DelayQueue API
Spring Boot - Runners
Spring 5 and Servlet 4 – The PushBuilder
Intro to Inversion of Control and Dependency Injection with Spring
The DAO with Spring and Hibernate
Java – Byte Array to Reader
Dynamic Proxies in Java
Configure a RestTemplate with RestTemplateBuilder
Hướng dẫn Java Design Pattern – Bridge
Giới thiệu JDBC Connection Pool
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
A Guide To UDP In Java
Java Program to Perform LU Decomposition of any Matrix
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Giới thiệu về Stream API trong Java 8
Java Program to implement Bit Matrix
Java String Conversions
Spring Data JPA and Null Parameters
Hướng dẫn Java Design Pattern – Transfer Object
New Features in Java 11
Implementing a Runnable vs Extending a Thread
Guava CharMatcher
Java Program to implement Circular Buffer