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:
Java – Write to File
Java – Write a Reader to File
New Features in Java 9
Java Program to Implement Graph Coloring Algorithm
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Batch Processing with Spring Cloud Data Flow
New Features in Java 15
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Chương trình Java đầu tiên
Posting with HttpClient
Java Program to Implement Aho-Corasick Algorithm for String Matching
Java Program to Check Whether a Given Point is in a Given Polygon
Using Spring ResponseEntity to Manipulate the HTTP Response
Binary Numbers in Java
Java Web Services – JAX-WS – SOAP
Spring 5 and Servlet 4 – The PushBuilder
Shuffling Collections In Java
Spring Data MongoDB – Indexes, Annotations and Converters
MyBatis with Spring
Rate Limiting in Spring Cloud Netflix Zuul
A Guide to ConcurrentMap
Guide to java.util.Formatter
Java Program to Implement Sparse Array
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java Program to Implement Splay Tree
Apache Commons Collections MapUtils
Daemon Threads in Java
Form Validation with AngularJS and Spring MVC
Check if a String is a Palindrome in Java
Registration – Activate a New Account by Email
Java Program to Implement Disjoint Set Data Structure
Java Program to Implement Expression Tree