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 Hash Tables chaining with Singly Linked Lists
Guide to the Synchronized Keyword in Java
How to Break from Java Stream forEach
Java Program to Implement Heap
Java Program to Perform the Unique Factorization of a Given Number
Spring Boot - Web Socket
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Implement Insertion Sort
Spring Boot - Apache Kafka
Spring Security with Maven
Introduction to Eclipse Collections
Returning Image/Media Data with Spring MVC
Bootstrap a Web Application with Spring 5
A Guide to the Java ExecutorService
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Base64 encoding và decoding trong Java 8
Instance Profile Credentials using Spring Cloud
Spring Security Login Page with React
Extract links from an HTML page
Java Program to Implement Attribute API
Zipping Collections in Java
Guide to the Volatile Keyword in Java
Guide to CountDownLatch in Java
Hướng dẫn sử dụng Java Annotation
Java Program to Check Cycle in a Graph using Topological Sort
An Example of Load Balancing with Zuul and Eureka
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Solve a Matching Problem for a Given Specific Case
Primitive Type Streams in Java 8
Bootstrapping Hibernate 5 with Spring
Java Program to Implement Double Order Traversal of a Binary Tree
Java Program to Implement AVL Tree