In the post Send e-mail in plain text using JavaMail, you know how to send an e-mail in plain text format. Although tt is possible to add HTML tags to the email's body content, that isn't enough to make the e-mail interpreted as an HTML e-mail in a web mail or an e-mail client program. So, instead of using:

Message msg = new MimeMessage(session);
msg.setText(message);

we should invoke the setContent(Object obj, String type) method of the MimeMessage object. The setContent() method specifies the mime type of the content explicitly, and for HTML format, the type parameter must be "text/html":

Message msg = new MimeMessage(session);
msg.setContent(message, "text/html");

That makes the difference! And following is source code of a sample program:

package net.codejava.mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class HtmlEmailSender {

	public void sendHtmlEmail(String host, String port,
			final String userName, final String password, String toAddress,
			String subject, String message) throws AddressException,
			MessagingException {

		// sets SMTP server properties
		Properties properties = new Properties();
		properties.put("mail.smtp.host", host);
		properties.put("mail.smtp.port", port);
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.starttls.enable", "true");

		// creates a new session with an authenticator
		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(userName, password);
			}
		};

		Session session = Session.getInstance(properties, auth);

		// creates a new e-mail message
		Message msg = new MimeMessage(session);

		msg.setFrom(new InternetAddress(userName));
		InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
		msg.setRecipients(Message.RecipientType.TO, toAddresses);
		msg.setSubject(subject);
		msg.setSentDate(new Date());
		// set plain text message
		msg.setContent(message, "text/html");

		// sends the e-mail
		Transport.send(msg);

	}

	/**
	 * Test the send html e-mail method
	 *
	 */
	public static void main(String[] args) {
		// SMTP server information
		String host = "smtp.gmail.com";
		String port = "587";
		String mailFrom = "your-email-address";
		String password = "your-email-password";

		// outgoing message information
		String mailTo = "your-friend-email-address";
		String subject = "Hello my friend";

		// message contains HTML markups
		String message = "<i>Greetings!</i><br>";
		message += "<b>Wish you a nice day!</b><br>";
		message += "<font color=red>Duke</font>";

		HtmlEmailSender mailer = new HtmlEmailSender();

		try {
			mailer.sendHtmlEmail(host, port, mailFrom, password, mailTo,
					subject, message);
			System.out.println("Email sent.");
		} catch (Exception ex) {
			System.out.println("Failed to sent email.");
			ex.printStackTrace();
		}
	}
}

 

In a mail client like Outlook, the e-mail would look like the following screenshot:

html email message

NOTES:

You need to use JavaMail jar files. If you use Maven, add the following dependency to the pom.xml file:

<dependency>
	<groupId>com.sun.mail</groupId>
	<artifactId>javax.mail</artifactId>
	<version>1.6.2</version>
</dependency>

This will add the javax.mail-VERSION.jar and activation-VERSION.jar to the project's classpath. If you have to add them manually, download from JavaMail Project page.

 

Other JavaMail Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (HtmlEmailSender.java)HtmlEmailSender.java[Java source code]2 kB

Add comment

   


Comments 

#34John Patrick2021-04-25 12:39
hello moto how are what r u doin'
Quote
#33Jean-Marie2020-12-29 13:21
hello Vimal, do you have an answer to my question by chance? I haven't always found a way out yet.
Jean-Marie
Quote
#32Vimal2020-12-28 08:01
Hi I am vimal how r u
Quote
#31Milon2020-09-26 09:59
Messenger squire code
Quote
#30Jasen Burkett2020-07-01 11:12
I am trying to figure out how to take 9 form fields and have those 9 fields data to be placed into the message body of the email when sent.

Is this something you might know how to do? I can send the form now, fine, but only the last field sends in the body as the rest are over written as it goes through them.
Quote