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 Find the Nearest Neighbor Using K-D Tree Search
Java Program to Implement Lloyd’s Algorithm
@Order in Spring
Sử dụng CyclicBarrier trong Java
Java Program to Implement AVL Tree
Generating Random Numbers in a Range in Java
A Guide to JUnit 5
Hướng dẫn Java Design Pattern – Interpreter
Weak References in Java
Tips for dealing with HTTP-related problems
Serialization và Deserialization trong java
Quick Guide on Loading Initial Data with Spring Boot
Spring REST API with Protocol Buffers
Java Program to Implement Sorted List
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Java Program to Implement Hash Tables Chaining with Binary Trees
Jackson – Change Name of Field
Merging Streams in Java
Java Program to Implement Suffix Array
Predicate trong Java 8
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Byte Array to InputStream
Using JWT with Spring Security OAuth (legacy stack)
Spring Boot Actuator
Java Program to Find Nearest Neighbor for Static Data Set
Java Program to Implement Insertion Sort
How to Read HTTP Headers in Spring REST Controllers
Java Program to Find Path Between Two Nodes in a Graph
Guide to Spring Cloud Kubernetes
Spring 5 and Servlet 4 – The PushBuilder
What is Thread-Safety and How to Achieve it?