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:
What is a POJO Class?
Copy a List to Another List in Java
Java Program to Implement Find all Back Edges in a Graph
Examine the internal DNS cache
Java Program to Implement Pagoda
A Quick Guide to Spring Cloud Consul
Spring Boot - OAuth2 with JWT
Java Program to Implement Queue using Two Stacks
Explain about URL and HTTPS protocol
Spring Boot - Eureka Server
HttpClient Connection Management
Java Program to Search for an Element in a Binary Search Tree
Prevent Cross-Site Scripting (XSS) in a Spring Application
Java Program to Perform Stooge Sort
Java Program to Implement Sorted Circularly Singly Linked List
An Introduction to ThreadLocal in Java
Guide to CountDownLatch in Java
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Java Program to Compute the Volume of a Tetrahedron Using Determinants
The Java 8 Stream API Tutorial
Java Program to Implement the Hill Cypher
Hướng dẫn Java Design Pattern – Command
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Java Program to Implement Shell Sort
Show Hibernate/JPA SQL Statements from Spring Boot
Map Interface trong java
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Java Program to Perform the Unique Factorization of a Given Number
Request Method Not Supported (405) in Spring
Java Program to Implement Double Order Traversal of a Binary Tree