How to Send Mail using JSP?

Java Mail API needs to be used to send mail using JSP. Java Mail API is used to compose, read and write messages.You need to include jars javax.mail and javax.mail.activation to send email using JSP. These packages contain core classes of Java Mail API. Java Mail API provides classes which constitute a mail system. Java Mail is used to receive email via SMTP, POP3 and IMAP protocols.

  • SMTP Protocol: It stands for Simple Mail Transfer Protocol which is used for transmission of mail across Internet Protocol (IP).It is also used for sending mail between servers. It can send messages to one server to another using POP or IMAP.
  • POP Protocol: It stands for Post Office Protocol. It is used to retrieve mail from remote server. There are two types in POP. The First one is called POP2 which can be used to send mail using SMTP protocol. The second one is called POP3 which can be used to send mail with or without SMTP protocol.
  • IMAP Protocol: It stands for Internet Message Access Protocol. Like POP protocol it is also used to retrieve mail from remote server.

Following steps are used to send mail using Java Mail API.

  • Get the Session Object: To send email Java Mail API uses send () method on Transport class. This can be achieved by using JAVAMail Session object.
  • Compose the message: Here we can write contents of message in body .The setSubject () method is used to set subject line and the setText () method is used to set content of the body. The javax.mail. Message package is used to compose the message.
  • Send the message: We can send the message by using package javax.mail. Transport.

Now let us write a simple program to send an email. Write a JSP called sendemail.jsp. This file contains the logic to send email. Include jars activation.jar and javax.mail.jar in the WEB-INF/lib folder.

To send email you need to have a SMTP server that is responsible foe sending emails. Using JangoSMPT server (online) is one of the methods to get SMTP server. Other method may be install a SMPT server like postfix on your machine or use SMTP server provided by gmail,yahoo etc.
Via JangoSMPT server server emails are sent to our destination email address. You can create an account by visiting this site and configure your email adress.

Listing 1: sendemail.jsp

<%@ page import="*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
	String result;
	//Recipient's email ID needs to be mentioned.
	String to = "";

	// Sender's email ID needs to be mentioned
	String from = "";
	final String username = "someusername";
	final String password = "password";

	// Assuming you are sending email through
	String host = "";

	Properties props = new Properties();
	props.put("mail.smtp.auth", "true");
	props.put("mail.smtp.starttls.enable", "true");
	props.put("", host);
	props.put("mail.smtp.port", "25");

	//Get the Session object.
	Session mailSession = Session.getInstance(props,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(username,

	try {
		// Create a default MimeMessage object.
		Message message = new MimeMessage(mailSession);

		// Set From: header field of the header.
		message.setFrom(new InternetAddress(from));

		// Set To: header field of the header.

		// Set Subject: header field
		message.setSubject("Testing Subject");

		// Now set the actual message
		message.setText("Hello, this is sample for to check send "
				+ "email using JavaMailAPI in JSP ");

		// Send message

		System.out.println("Sent message successfully....");
		result = "Sent message successfully....";

	} catch (MessagingException e) {
		result = "Error: unable to send message....";

<title>Send Email using JSP</title>
		<h1>Send Email using JSP</h1>
	<p align="center">
			out.println("Result: " + result + "\n");

Execute the jsp file sendemail.jsp in Eclipse by selecting Run As > Run On Server an output as below would be seen:

If you check the inbox of “”, you can see a new message with the text:

Hello, this is sample for to check send  email using JavaMailAPI in JSP

Previous Tutorial : Upload File using JSP || Next Tutorial : JSP Expression Language

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!