New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs

Email Sending using Servlet

In this example we will see how to send an email in Servlet application. We will be using the JavaMail API that provides all the classes required for sending an email. JavaMail API encapsulates two important packages javax.mail and javax.mail.internet. These packages provide classes that can be used to send and recieve simple emails. You simply need an Internet connection to send email using this simple Application.

Following are the names of Files to be created:

  • index.html will get the input from user
  • servlet file will control the request and response. It will invoke send() of SendMail class that we have created to send the mail.
  •, a java class that contains method to send mail.


<form action="mail" method="post">
  To:<input type="text" name="to"  /><br/>
  Subject:<input type="text" name="subject"  /><br/>
  Message:<input type="text" name="message"  /><br/>
  Your Email id:<input type="text" name="user" ><br/>
  Password<input type="password" name="pass"   /><br/>
  <input type="submit" value="send" />

import javax.servlet.*;
import javax.servlet.http.*;

public class MailApp extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        String to = request.getParameter("to");
        String subject = request.getParameter("subject");
        String message =  request.getParameter("message");
        String user = request.getParameter("user");
        String pass = request.getParameter("pass");
        SendMail.send(to,subject, message, user, pass);
        out.println("Mail send successfully");

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SendMail
    public static void send(String to, String sub, 
                         String msg, final String user,final String pass)
        //create an instance of Properties Class   
        Properties props = new Properties();
        /*  Specifies the IP address of your default mail server
     	      for e.g if you are using gmail server as an email sever
            you will pass as value of mail.smtp host. 
            As shown here in the code. 
            Change accordingly, if your email id is not a gmail id
        props.put("", "");
        // below mentioned mail.smtp.port is optional 
        props.put("mail.smtp.port", "587");		
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        /* Pass Properties object(props) and Authenticator object   
           for authentication to Session instance 

        Session session = Session.getInstance(props,new javax.mail.Authenticator()
            protected PasswordAuthentication getPasswordAuthentication()
  	 	         return new PasswordAuthentication(user,pass); 

        try {
     	      /*  Create an instance of MimeMessage, 
     	          it accept MIME types and headers 
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(user));
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

            /* Transport class is used to deliver the message to the recipients */
        catch(Exception e) {


<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">


In this example the SendMail class is the main class, as it will setup a connection with the Email Server, it will authenticate the username and password and will then use the information entered in the form like, receiver's email address, the email text etc and will send the email.

MailApp is the servlet ti which the HTML page form will submit the inputs, this Servlet will fetch all the parameters and call the send() method of SendMail Java class.