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:
Prevent Cross-Site Scripting (XSS) in a Spring Application
Find the Registered Spring Security Filters
Lớp Properties trong java
Getting the Size of an Iterable in Java
Java Program to Implement Cubic convergence 1/pi Algorithm
Java Program to Implement Sieve Of Atkin
Spring Boot - Web Socket
Spring Boot - Logging
Examine the internal DNS cache
Java Program to Implement Euclid GCD Algorithm
Getting a File’s Mime Type in Java
MyBatis with Spring
Java Program to Implement Graph Structured Stack
Java Program to Find a Good Feedback Edge Set in a Graph
Hướng dẫn sử dụng Printing Service trong Java
Java Program to Construct an Expression Tree for an Infix Expression
Java Program to Implement Bubble Sort
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
REST Web service: Basic Authentication trong Jersey 2.x
HTTP Authentification and CGI/Servlet
Java Program to Check whether Undirected Graph is Connected using DFS
Spring Security OAuth Login with WebFlux
Hướng dẫn Java Design Pattern – Chain of Responsibility
Java Program to Implement Levenshtein Distance Computing Algorithm
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Generate Date Between Given Range
Spring Data – CrudRepository save() Method
Comparing Objects in Java
Java Program to Implement Queue using Linked List
Java 8 Stream findFirst() vs. findAny()
Upload and Display Excel Files with Spring MVC
Auditing with JPA, Hibernate, and Spring Data JPA