How To Send a Plain Text EMail in Java using JavaMail API ?

Java Mail API is used to create your own email client. This class provides the code required to model your email client similar to Outlook, Hotmail and many others. In JavaMail you’ll find APIs and provider implementations allowing you to develop fully functional email client applications. Below java program shows you how to send a plain text email using JavaMail API.

/**
 * Created on Apr 18, 2014. Copyright(c) https://kodehelp.com All Rights Reserved.
 */
package com.kodehelp.javax.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author https://kodehelp.com
 *
 */
public class SendEmail {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final String FROM = "demo@ kodehelp.com"; //From email address
		final String TO = "demo@ kodehelp.com"; // To email Address
		final String SUBJECT = "Email sent using Java Mail API";
		final String CONTENT = "Hello, This is email is the test email sent from Kodehelp.com's Java Mail API example.";

		final String SMTP_HOST = "smtp.gmail.com";
		final String SMTP_PORT = "465";
		final String EMAIL_PROTOCOL = "smtp";

		//Properties is used to store the email configurations such as mail protocol, SMTP host, SMTP port and many others
		Properties prop = new Properties();
		prop.put("mail.smtp.host", SMTP_HOST);
		prop.put("mail.smtp.port", SMTP_PORT);
		prop.put("mail.transport.protocol", EMAIL_PROTOCOL);
		prop.put("mail.smtp.ssl.enable","true");
		prop.put("mail.debug", "true");

		Session session = Session.getDefaultInstance(prop);

		try {
			InternetAddress fromAddress = new InternetAddress(FROM);
			InternetAddress toAddress = new InternetAddress(TO);

			Message message = new MimeMessage(session);
			message.setFrom(fromAddress);
			message.setRecipient(Message.RecipientType.TO, toAddress);
			message.setSubject(SUBJECT);
			message.setText(CONTENT);
			Transport.send(message, "demo@kodehelp .com", "password");

		}catch(Exception e){
			e.printStackTrace();
		}

	}

}