Java Mail API

  • Java Mail is a open-source API.
  • The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications.
  • The javax.mail package defines classes that are common to all mail systems
  • With the help of Java API you can send, receive, forward, reply of the Mail.
  • JavaMail is a Java API used to receive and send Email via SMTP, POP3 and IMAP. JavaMail is built into the JavaEE platform, but also provides an optional package for use in JavaSE. Mail.smtp.startttls.enable(Start Transport Layer Security)

Steps for Using Java mail API for Sending Mail

Step1: Create Properties

Create Properties and put the host name and mail domain name as below :

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
// props.put("key", "value");
props.put("mail.smtp.auth", "true");
props.setProperty("mail.smtp.port", "587");		
props.put("mail.smtp.starttls.enable", "true");

Step2:Create session

Create session and give the email id as sender id and give the password for authenticate

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() 
{
	protected PasswordAuthentication
		getPasswordAuthentication() 
	{
		return new PasswordAuthentication(
		"jtechies@jtechies.com", "jtechies123");
			// SenderID and Password.
	}
});

Step3:Create MimeMessage and pass the session to MimeMessage

Create MimeMessage class object and pass the session and set the message which you want to send .

MimeMessage message = new
	MimeMessage(session);
message.setFrom(new InternetAddress
	("jtechies@jtechies.com")); // Sender Id.

Step 4. Add recipient and message
message.addRecipient(Message.RecipientType.TO,
	new InternetAddress(to));
message.setSubject("Hello!");
message.setText("Testing Java Application.......
	Mail from jtechies.com ");

Step5:Allow to Transport layer to send
Transport.send(message);
System.out.println("message sent successfully");

Java Mail Complete Example

package  com.jtechies.mail 
import java.util.Properties;
import java.util.Properties;
import javax.mail.Message;
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 SendMail 
{
	public SendMail() 
	{
		String to = "";// Receiver Address.
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		// props.put("key", "value");
		props.put("mail.smtp.auth", "true");
		props.setProperty("mail.smtp.port", "587");		
		props.put("mail.smtp.starttls.enable", "true");
		Session session = Session.getDefaultInstance
			(props, new javax.mail.Authenticator() 
		{
			protected PasswordAuthentication
				getPasswordAuthentication() {
				return new PasswordAuthentication(
				"jtechies@jtechies.com", "jtechies123");
					// SenderID and Password.
			}
		});
		try {
			MimeMessage message = new 
				MimeMessage(session);
			message.setFrom(new InternetAddress
				("jtechies@jtechies.com"));
				// Sender Id.
			message.addRecipient
			(Message.RecipientType.TO, 
			new InternetAddress(to));
			message.setSubject("Hello!");
			message.setText
				("Testing Java Application.......
				Mail from jtechies.com");
			Transport.send(message);
			System.out.println
				("message sent successfully");
		} 
		catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
	public static void main(String[] args)
		throws MessagingException {
	new SendMail();
	}
}