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:
Consuming RESTful Web Services
New Features in Java 13
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Hashing a Password in Java
Comparing Long Values in Java
Java Program to Implement the One Time Pad Algorithm
Anonymous Classes in Java
Remove the First Element from a List
Deque và ArrayDeque trong Java
More Jackson Annotations
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Tips for dealing with HTTP-related problems
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Spring Cloud AWS – Messaging Support
Java Program to Compute Cross Product of Two Vectors
Batch Processing with Spring Cloud Data Flow
Guide to the ConcurrentSkipListMap
Java Program to Implement Bubble Sort
Spring Cloud AWS – RDS
Auditing with JPA, Hibernate, and Spring Data JPA
Quick Guide to java.lang.System
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Returning Custom Status Codes from Spring Controllers
Encode a String to UTF-8 in Java
ArrayList trong java
HandlerAdapters in Spring MVC
RestTemplate Post Request with JSON
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Java Program to Implement Hamiltonian Cycle Algorithm
HttpClient with SSL
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph