<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">In this example we will see how to send email in Servlet application. JavaMail API that provides all classes require for sending an email. JavaMail API encapsulate two important package javax.mail and javax.mail.internet. These packages provides classes that can be used to send and receive simple emails. You simply need an Internet connection to send email using this simple Application.</p>
<ul style="text-align: left;">
<li>index.html will get the input from user</li>
<li>MailApp.java servlet file will control the request and response. It will invoke send() of SendMail class that we have created to send the mail.</li>
<li>SendMail.java, a java class that contains method to send mail.</li>
</ul>
<p><b>index.html</b></p>
<pre class="highlight"><;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" />; 
 <;/form>;</pre>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p><b>MailApp.java</b></p>
<pre class="highlight">import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
 
 
public class MailApp extends HttpServlet { 
 
 
 protected void doPost(HttpServletRequest request, HttpServletResponse response) 
 throws ServletException, IOException { 
 response.setContentType("text/html;charset=UTF-8"); 
 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"); 
 
 } 
} 
</pre>
<p><b>SendMail.java</b></p>
<pre class="highlight">import java.io.*; 
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 smtp.gmail.com as value of mail.smtp host. As shown here in the 
 coding. Change accordingly, if your email id is not an gmail id*/ 
 props.put("mail.smtp.host", "smtp.gmail.com"); 
 
 props.put("mail.smtp.port", "587"); //this is optional 
 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)); 
 message.setSubject(sub); 
 message.setText(msg); 
 
 /* Transport class is used to deliver the message to the recipients */ 
 Transport.send(message); 
 
 
 }catch(Exception e) 
 { 
 e.printStackTrace(); 
 } 
 } 
 
} 
</pre>
<p><b>web.xml</b></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
 
<;web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">; 
 
 <;servlet>; 
 <;servlet-name>;mail<;/servlet-name>; 
 <;servlet-class>;MailApp<;/servlet-class>; 
 <;/servlet>; 
 
 <;servlet-mapping>; 
 <;servlet-name>;mail<;/servlet-name>; 
 <;url-pattern>;/mail<;/url-pattern>; 
 <;/servlet-mapping>; 
 
 <;welcome-file-list>; 
 <;welcome-file>;index.html<;/welcome-file>; 
 <;/welcome-file-list>; 
<;/web-app>; 
 
</pre>
</div>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><b>Servlet Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/java-servlets-overview/">Java Servlets Overview</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-life-cycle/">Servlet Life Cycle</a></b></li>
<li><b><a href="https://dineshonjava.com/hello-world-example-in-servlet-interface/">Servlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-servletconfig-and-cervletcontext-in-java-servlet/ "> Difference between ServletConfig and ServletContext<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/"> Difference between GenericServlet and HttpServlet<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-web-application-servlet/">What is web application?</a></b></li>
<li><b><a href="https://dineshonjava.com/advantages-of-servlets-over-cgi/">Advantages of Servlets over CGI</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/">GenericServlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/requestdispatcher-interface/">RequestDispatcher Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servletconfig-interface/">ServletConfig</a></b></li>
<li><b><a href="https://dineshonjava.com/servletcontext-interface/">ServletContext </a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-filters/">Servlet Filter Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servlets-database-access/">Database Access Example using Sevlet</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-upload-file-example/">File Uploading Example using Servlet</a></b></li>
</ol>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/servlets-auto-page-refresh/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-servlets-overview/">Index </a>|| >;>;<a href="https://dineshonjava.com/java-mail-api-tutorial/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/servlets-auto-page-refresh/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/servlets-database-access/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '253'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…