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:
Adding Parameters to HttpClient Requests
Practical Java Examples of the Big O Notation
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
A Custom Data Binder in Spring MVC
Java Program to Implement Bubble Sort
Spring Cloud AWS – RDS
ETags for REST with Spring
Spring Boot - OAuth2 with JWT
How to Get All Dates Between Two Dates?
Java Program to Implement Self organizing List
Guide to Java 8 groupingBy Collector
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Quick Guide to the Java StringTokenizer
Hướng dẫn sử dụng Printing Service trong Java
Comparing Two HashMaps in Java
Create Java Applet to Simulate Any Sorting Technique
Java 8 Streams peek() API
The Spring @Controller and @RestController Annotations
Configure a Spring Boot Web Application
Java Program to Implement Park-Miller Random Number Generation Algorithm
Send email with authentication
Overflow and Underflow in Java
Ignore Null Fields with Jackson
Introduction to Using Thymeleaf in Spring
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Java Program to Represent Graph Using 2D Arrays
Java Program to Decode a Message Encoded Using Playfair Cipher
Function trong Java 8
Java Program to Perform Search in a BST
Java Program to Implement RoleUnresolvedList API
Java Program to Construct an Expression Tree for an Postfix Expression