Pr.Pg Next Pg

Java Mail API tutorials

  • You will need JavaMail API installed on your machine for sending mail from JSP program.

  • You can download latest version of JavaMail (Version 1.2) from Java's standard website.

  • Download and unzip these files, in the newly created top level directories you will find a number of jar files for the applications. You need to add mail.jar in your CLASSPATH.

  • If you are using Netbeans then below is the steps to add mail.jar:

  1. Open Netbeans then go to File Menu à New Project à Now from category tab Select “Java Web” then from project category select “Web Application” and click Next, then give appropriate Project Name and click on Next then select “Server” and click Finish, now the opened Netbeans web application project will look like below:

Library installation

  1. Now Right click on “Libraries” and select “Add JAR/Folder..” from Projects tab as below:

Library installation


  1. Now in “Add JAR/Folder” dialog box locate your javax.mail.jar file from dropdown list and then select the javax.mail.jar file and click “Open” as below:

Library Installation


JSP program to send e-mail and it’s data coming from Mail.html

Sam Sir

<%--Program name MailProcess.jsp --%>

<%-- JSP program to send e-mail and it’s data coming from Mail.html --%>


<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="javax.mail.*"%>

<%@page import="javax.mail.internet.*" %>

<%@page import="*" %>

<%@page import="java.util.Properties" %>

<%@page import="Authentication.SMTPAuthenticator" %>



<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Mail Result</title>




String from = request.getParameter("from");

String to = request.getParameter("to");

String subject = request.getParameter("subject");

String message = request.getParameter("message");

String login = request.getParameter("login");

String password = request.getParameter("password");

try {

Properties props = new Properties();

props.setProperty("", "");

props.setProperty("mail.smtp.port", "587");

props.setProperty("mail.smtp.auth", "true");

props.setProperty("mail.smtp.starttls.enable", "true");


Authenticator auth = new SMTPAuthenticator(login, password);

Session sess=Session.getInstance(props, auth);

MimeMessage msg = new MimeMessage(sess);



msg.setFrom(new InternetAddress(from));

msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));


out.println("Send Successfully..");

} catch (Exception ex) {

out.println("Error :"+ex.getMessage());









Java class for authenticating user for sending e-mail

Sam Sir

//Program name

//Java class for authenticating user for sending e-mail

package Authentication;


import javax.mail.*;

public class SMTPAuthenticator extends Authenticator {

private PasswordAuthentication authentication;


public SMTPAuthenticator(String login, String password) {

authentication = new PasswordAuthentication(login, password);


protected PasswordAuthentication getPasswordAuthentication() {

return authentication;







Note: Create a Java package Authentication and placed SMTPAuthenticator class in Authentication package.



HTML program for designing UI for sending e-mail

Sam Sir

<!-- Mail.html -->

<!-- HTML program for designing UI for sending e-mail -->



<title>Sending email</title>



<h3>JavaMail API</h3>


<form method="post" action="MailProcess.jsp">




<td><input type="text" name="login"></td>




<td><input type="password" name="password"></td>




<td><input type="text" name="from"></td>




<td><input type="text" name="to"></td>




<td><input type="text" name="subject"></td>




<td><textarea cols="25" rows="8" name="message"></textarea></td>




<input type="reset" value="Reset"> &nbsp; <input type="submit" value="submit">





JSP Mail


JSP Mail

Pr.Pg border                                              Next Pg