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:
Hướng dẫn Java Design Pattern – Interpreter
Create Java Applet to Simulate Any Sorting Technique
The DAO with JPA and Spring
Biểu thức Lambda trong Java 8 – Lambda Expressions
Java Program to Implement Quick sort
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Spring Boot: Customize the Jackson ObjectMapper
Automatic Property Expansion with Spring Boot
Java 8 Predicate Chain
Java – Write a Reader to File
Java Program to Implement CopyOnWriteArrayList API
A Custom Media Type for a Spring REST API
Adding Shutdown Hooks for JVM Applications
Sử dụng CyclicBarrier trong Java
Java Program to Implement Regular Falsi Algorithm
Java toString() Method
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Annotation trong Java 8
Java Program to Compute the Area of a Triangle Using Determinants
Request a Delivery / Read Receipt in Javamail
Spring Data – CrudRepository save() Method
The Difference Between map() and flatMap()
Check If a File or Directory Exists in Java
Jackson Annotation Examples
Sort a HashMap in Java
Generating Random Dates in Java
Feign – Tạo ứng dụng Java RESTful Client
An Example of Load Balancing with Zuul and Eureka
Java Program to Implement Knight’s Tour Problem
Java Program to Implement Stack
Functional Interfaces in Java 8
Java Program to Implement LinkedBlockingQueue API