Notice: Constant WP_MEMORY_LIMIT already defined in /home/javawith/public_html/ on line 107

Notice: Constant WP_MAX_MEMORY_LIMIT already defined in /home/javawith/public_html/ on line 108
Send html content in email in java - CodesJava


Easy learning with example program codes

Send HTML content in email using JavaMail API

Notice: Undefined variable: footnotelist in /home/javawith/public_html/ on line 475

We are explaining here an example of sending HTML content in email using JavaMail API.

Steps of sending HTML content in email using JavaMail API:

    1. Get a session instance from getDefaultInstance() or getInstance() method of Session class.
    2. Create a message we have to pass session object in MimeMessage class constructor.
    3. Use the InternetAddress class to set the information of sender and recipient.
    4. Complose the message by using MimeMessage class methods like setFrom(Address address), addRecipients(Message.RecipientType type, String addresses), setSubject(String subject), setText(String textmessage) etc.
    5. Use send() method of javax.mail.Transport class to send the message.


import java.util.*;  
import javax.mail.*;  
import javax.mail.internet.*;  
 * This class is used to send email with HTML content.
 * @author codesjava
public class SendEmailWithHTML { 
 final String senderEmailId = "";
 final String senderPassword = "****";
 final String emailSMTPserver = "";
public SendEmailWithHTML(String receiverEmail, 
		String subject, String htmlContent, 
		String contentType) {		
	Properties props = new Properties();
	props.put("mail.smtp.auth", "true");
	props.put("", emailSMTPserver);
  try { 			
	Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getInstance(props, auth);
	Message message = new MimeMessage(session);
	message.setFrom(new InternetAddress(senderEmailId));
	message.setContent(htmlContent, contentType);
	System.out.println("Email send successfully."); 
  } catch (Exception e) {
    System.err.println("Error in sending email.");
private class SMTPAuthenticator extends 
  javax.mail.Authenticator {
    public PasswordAuthentication 
      getPasswordAuthentication() {
        return new PasswordAuthentication(senderEmailId, 
public static void main(String[] args) {
	new SendEmailWithHTML ("", "Test Email", 
	 "<h1>This is a html content test email.</h1>", "text/html");


Email send successfully.

Download this example.
Next Topic: Send inline image with example.
Previous Topic: Send email with attachment with example.


Please follow and like us:

Copyright © 2019 CodesJava Protection Status