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 the ResourceBundle
Debug a JavaMail Program
Java Program to Print only Odd Numbered Levels of a Tree
Guide to Java 8 groupingBy Collector
Spring Boot - Apache Kafka
How to Change the Default Port in Spring Boot
Java Program to Evaluate an Expression using Stacks
Send an email with an attachment
Summing Numbers with Java Streams
Java Program to Implement Disjoint Set Data Structure
Class Loaders in Java
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Java Program to Implement Counting Sort
Build a REST API with Spring and Java Config
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Find Nearest Neighbor Using Linear Search
Spring Boot - Zuul Proxy Server and Routing
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Custom Thread Pools In Java 8 Parallel Streams
How to Manually Authenticate User with Spring Security
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Hướng dẫn sử dụng Java Generics
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Mệnh đề if-else trong java
Java – Reader to InputStream
Spring REST API with Protocol Buffers
Registration – Activate a New Account by Email
A Guide to the ViewResolver in Spring MVC
JUnit 5 for Kotlin Developers
Java – Create a File
Java Program to Implement Disjoint Sets
TreeSet và sử dụng Comparable, Comparator trong java