How to send HTML Email Body Using Java Gmail SMTP
Table of Contents
In this article, I am going to share How to send HTML Email Body Using Java Gmail SMTP. Send an email with an HTML body using java through the Gmail SMTP server.
The previous article explained how to send an email with a text body using Java through the Gmail SMTP server.
How to send HTML Email Body Using Java Gmail SMTP
Before going to write a program, we need to know the Gmail SMTP server details.
To send Html body, you need to create MimeMessage and setContent as “text/html; charset=utf-8”
MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.addRecipient(RecipientType.TO, new InternetAddress(ToAddress)); mimeMessage.setSubject(EmailSubject); // setting HTML message body mimeMessage.setContent(EmailBody,"text/html; charset=utf-8");
GmailHTMLMsgSend.java
package com.narayanatutorial.gmail; import java.util.Properties; import javax.mail.Message.RecipientType; import javax.mail.Authenticator; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class GmailHTMLMsgSend { Properties properties; Session session; MimeMessage mimeMessage; String USERNAME = "XXXXXXX@gmail.com"; String PASSWORD = "XXXXXXX"; String HOSTNAME = "smtp.gmail.com"; String STARTTLS_PORT = "587"; boolean STARTTLS = true; boolean AUTH = true; public static void main(String args[]) throws MessagingException { String EmailSubject = "Subject:HTML Subject Body Test"; String EmailBody = "<b>Text Message Body: Hello World</b>"; String ToAddress = "XXXXXXX@gmail.com"; GmailHTMLMsgSend gmailHTMLMsgSend = new GmailHTMLMsgSend(); gmailHTMLMsgSend.sendGmail(EmailSubject, EmailBody, ToAddress); } public void sendGmail(String EmailSubject, String EmailBody, String ToAddress) { try { properties = new Properties(); properties.put("mail.smtp.host", HOSTNAME); // Setting STARTTLS_PORT properties.put("mail.smtp.port", STARTTLS_PORT); // AUTH enabled properties.put("mail.smtp.auth", AUTH); // STARTTLS enabled properties.put("mail.smtp.starttls.enable", STARTTLS); // Authenticating Authenticator auth = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(USERNAME, PASSWORD); } }; // creating session session = Session.getDefaultInstance(properties, auth); // create mimemessage mimeMessage = new MimeMessage(session); mimeMessage.addRecipient(RecipientType.TO, new InternetAddress(ToAddress)); mimeMessage.setSubject(EmailSubject); // setting HTML message body mimeMessage.setContent(EmailBody,"text/html; charset=utf-8"); // sending mail Transport.send(mimeMessage); System.out.println("Mail Send Successfully"); } catch (Exception e) { e.printStackTrace(); } } }
You can get full source code from Github.
Thanks for reading the article.
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.