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 Implement Floyd-Warshall Algorithm
Assertions in JUnit 4 and JUnit 5
Spring Boot Integration Testing with Embedded MongoDB
Java – Convert File to InputStream
Introduction to Using Thymeleaf in Spring
Spring Security Remember Me
Mockito and JUnit 5 – Using ExtendWith
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Disable DNS caching
Receive email using POP3
The Guide to RestTemplate
Java Program to Implement Patricia Trie
Java Program to Implement Sieve Of Sundaram
Spring Security Login Page with React
Java Program to Implement Max Heap
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Versioning a REST API
Hướng dẫn Java Design Pattern – Service Locator
Java Program to Generate Random Numbers Using Probability Distribution Function
ETags for REST with Spring
Java Program to Perform Naive String Matching
LinkedHashSet trong Java hoạt động như thế nào?
Hướng dẫn Java Design Pattern – Dependency Injection
Beans and Dependency Injection
Number Formatting in Java
Java Program to Implement Best-First Search
Java Program to Implement Knight’s Tour Problem
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Introduction to Spring Data JPA
Merging Streams in Java
Java Program to Check whether Graph is a Bipartite using DFS
Java Program to Check whether Undirected Graph is Connected using BFS