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:
Collect a Java Stream to an Immutable Collection
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Java Program to Implement Merge Sort Algorithm on Linked List
Spring @RequestMapping New Shortcut Annotations
Guide to the Synchronized Keyword in Java
How to Set TLS Version in Apache HttpClient
Java Program to Implement Floyd Cycle Algorithm
Java Program to Implement vector
Java Convenience Factory Methods for Collections
Java Program to Implement Interval Tree
Java Program to Find Nearest Neighbor Using Linear Search
Introduction to Spring Data JDBC
Java Program to Implement Bubble Sort
Spring Boot - Eureka Server
Chuyển đổi Array sang ArrayList và ngược lại
Checked and Unchecked Exceptions in Java
Spring Cloud Bus
StringBuilder vs StringBuffer in Java
Java InputStream to Byte Array and ByteBuffer
LinkedHashSet trong Java hoạt động như thế nào?
Java Program to Generate Randomized Sequence of Given Range of Numbers
Guide to Java 8’s Collectors
Truyền giá trị và tham chiếu trong java
Java Program to Implement the Bin Packing Algorithm
Java Program to Implement Sieve Of Eratosthenes
Dockerizing a Spring Boot Application
How to Get All Dates Between Two Dates?
A Guide to HashSet in Java
Java Program to Implement Expression Tree
Java – Reader to String
Lớp TreeMap trong Java
Encode/Decode to/from Base64