Sending Email in JSP

Sending Email in JSP Application

In this article, we will learn how to develop Sending Email in JSP Application. Please read our previous article where we develop Auto Page Refresh Application in JSP.

In this example, we will send email using JSP. We can use certain protocols like SMTP protocol, POP Protocol, IMAP protocol to send Email using JSP. To send an email using JSP download JavaMail API and Java Activation Framework (JAF) on your machine. Firstly, download both and unzip these files where you will find number of jar files for both the applications. Also, we need to add mail.jar and activation.jar files in our CLASSPATH.

Send an HTML Email in JSP

In this example, we are using the setContent() method to set content whose argument is “text/html” to specify that the HTML content is included in the message. Make sure you have all the jar files of Java Mail API and JAF available in your CLASSPATH.

email.jsp
<%@ page import="java.io.*, 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 = "manisha@gmail.com";

// Sender's email ID needs to be mentioned
String from = "manisha@gmail.com";

// Assuming email sent from localhost
String host = "localhost";

// Get properties object
Properties properties = System.getProperties();

// Setup mail server
properties.setProperty("mail.smtp.host", host);

// Get Session object.
Session mailSession = Session.getDefaultInstance(properties);

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

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

 // Set To: header field
 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

 // Set Subject: header field
 message.setSubject("This is Subject Line");

 // Send the HTML message
 message.setContent("<h1>This is actual message</h1>", "text/html");

 // Send message
 Transport.send(message);
 result = "Sent message successfully....";
} catch (MessagingException mex) {
 mex.printStackTrace();
 result = "Error: unable to send message....";
}
%>

<html>
<head>
<title>Send HTML Email using JSP</title>
</head>
<body>
 <h1>Send Email using JSP</h1>
 <%
 out.println("Result: " + result + "\n");
 %>
</body>
</html>
Output

Sending Email in JSP Application

Sending Email with attachment in JSP

In this example, we will send an email with an attachment.

email.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*,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 = "manisha@gmail.com";

// Sender's email ID needs to be mentioned
String from = "manisha@gmail.com";

// Assuming you are sending email from localhost
String host = "localhost";

// Get system properties object
Properties properties = System.getProperties();

// Setup mail server
properties.setProperty("mail.smtp.host", host);

// Get the default Session object.
Session mailSession = Session.getDefaultInstance(properties);

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

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

 // Set To: header field of the header.
 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

 // Set Subject: header field
 message.setSubject("This is the Subject Line!");

 // Create the message part
 BodyPart messageBodyPart = new MimeBodyPart();

 // Fill the message
 messageBodyPart.setText("This is message body");

 // Create a multipart message
 Multipart multipart = new MimeMultipart();

 // Set text message part
 multipart.addBodyPart(messageBodyPart);

 // Part two is attachment
 messageBodyPart = new MimeBodyPart();

 String filename = "file.txt";
 DataSource source = new FileDataSource(filename);
 messageBodyPart.setDataHandler(new DataHandler(source));
 messageBodyPart.setFileName(filename);
 multipart.addBodyPart(messageBodyPart);

 // Send the complete message parts
 message.setContent(multipart);

 // Send message
 Transport.send(message);
 String title = "Send Email";
 result = "Sent message successfully....";
} catch (MessagingException mex) {
 mex.printStackTrace();
 result = "Error: unable to send message....";
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

 <h1>Send Attachment Email using JSP</h1>

 <p>
  <%
  out.println("Result: " + result + "\n");
  %>
 </p>
</body>
</html>
Output

Sending Email with attachment in JSP

Sending Email using Forms in JSP

In this example, we are using HTML Forms to accept email parameters and then will use the request object to get all the information. We are creating index.html where the user will enter the email parameters and after clicking on the submit button it will redirect the user to email.jsp page.

index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <form action="email.jsp" method="post">
  To :<input type="text" name="to" /> <br> <br>From :<input
   type="text" name="from" /> <br> <br> <input type="submit" />
 </form>
</body>
</html>
email.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*, 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 = request.getParameter("to");

// Sender's email ID needs to be mentioned
String from = request.getParameter("from");

// Assuming email sent from localhost
String host = "localhost";

// Get properties object
Properties properties = System.getProperties();

// Setup mail server
properties.setProperty("mail.smtp.host", host);

// Get Session object.
Session mailSession = Session.getDefaultInstance(properties);

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

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

 // Set To: header field
 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

 // Set Subject: header field
 message.setSubject("Enter Subject Line!");

 // Send the HTML message
 message.setContent("<h1>This is actual message</h1>", "text/html");

 // Send message
 Transport.send(message);
 result = "Sent message successfully....";
} catch (MessagingException mex) {
 mex.printStackTrace();
 result = "Error: unable to send message....";
}
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <h1>Send Email using JSP</h1>
 <%
 out.println("Result: " + result + "\n");
 %>
</body>
</html>
Output

Run your index.jsp page to get the following output:

Sending Email using Forms in JSP

Enter your details and click “Submit” button.

Sending Email in JSP Application

Here, in this article, we develop Sending Email in JSP Application and I hope you enjoy this Sending Email in JSP Application article.

Leave a Reply

Your email address will not be published.