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:
Giới thiệu Google Guice – Binding
Java Program to Implement Find all Cross Edges in a Graph
Java Program to Implement Radix Sort
Number Formatting in Java
Apache Camel with Spring Boot
Disable DNS caching
How to Read HTTP Headers in Spring REST Controllers
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Iterable to Stream in Java
Guide to the Synchronized Keyword in Java
Java Program to Implement Sorted Singly Linked List
Java Program to Implement the RSA Algorithm
Kết hợp Java Reflection và Java Annotations
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Web Services – JAX-WS – SOAP
Java Program to Implement Binomial Heap
Count Occurrences of a Char in a String
New Features in Java 14
Java Program to Perform LU Decomposition of any Matrix
LinkedHashSet trong Java hoạt động như thế nào?
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Spring Boot - Unit Test Cases
Uploading MultipartFile with Spring RestTemplate
Java Program to Perform Uniform Binary Search
Send an email using the SMTP protocol
Filtering and Transforming Collections in Guava
Logout in an OAuth Secured Application
Java Program to Implement Sorted Circular Doubly Linked List
Java Program to Check for balanced parenthesis by using Stacks
Checking for Empty or Blank Strings in Java
Java Program to Implement Unrolled Linked List
Java Program to Implement Selection Sort