WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JSP Send Email


Although using JSP to implement mail delivery is simple, you need the JavaMail API and you need to install the JavaBean Activation Framework.

Download and unzip these files. In the root directory, you will see a series of jar packages. Add the mail.jar package and the activation.jar package to the CLASSPATH variable.

Send a simple email

This example shows how to send a simple email from your machine. It assumes that localhost is connected to the network and capable of sending an email. At the same time, please confirm once again that the mail.jar package and activation.jar package have been added to the CLASSPATH variable.

<%@ 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
   String to = "abcd@gmail.com";

   // Sender's email
   String from = "mcmohd@gmail.com";

   // Suppose you are sending an email from a local host
   String host = "localhost";

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

   // Setting 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);
      // Setting From: Header field
      message.setFrom(new InternetAddress(from));
      // Setting To: Header field
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));
      // Set the Subject: header field 
      message.setSubject("This is the Subject Line!");
      //  Actual message of Setting now
      message.setText("This is actual message");
      // Send a message
      Transport.send(message);
      result = "Sent message successfully....";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message....";
   }
%>
<html>
<head>
<title>Send Email using JSP</title>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
<p align="center">
<% 
   out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>

Visit http now://localhost:8080/SendEmail.jsp, It will send an email toabcd@gmail.com and display the following results:

Send Email using JSP
Result: Sent message successfully....

If you want to send mail to multiple people, the methods listed below can be used to specify multiple email addresses:

void addRecipients(Message.RecipientType type, 
                   Address[] addresses)
throws MessagingException

The description of the parameters is as follows:

  • type: This value will be set to TO (recipient), CC, or BCC. CC stands for Carbon Copy and BCC stands for Blind Carbon Copy. The example program uses TO.
  • addresses: This is an array of email addresses. You need to use the InternetAddress () method when specifying email addresses.

Send an HTML email

This example sends a simple HTML email. It assumes that your localhost is connected to the network and capable of sending mail. At the same time, please confirm once again that the mail.jar package and activation.jar package have been added to the CLASSPATH variable.

This example is very similar to the previous example, but in this example we used the setContent () method, passing "text / html" as the second parameter to indicate that the message contains HTML content.

<%@ 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
   String to = "abcd@gmail.com";

   // Sender's email
   String from = "mcmohd@gmail.com";

   // Suppose you are sending an email from a local host
   String host = "localhost";

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

   // Setting 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);
      // Setting From: Header field
      message.setFrom(new InternetAddress(from));
      // Setting To: Header field
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));
      // Set the Subject: header field 
      message.setSubject("This is the Subject Line!");
     
      // Setting HTMLNews
      message.setContent("<h1>This is actual message</h1>",
                            "text/html" );
      // Send a 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>
<center>
<h1>Send Email using JSP</h1>
</center>
<p align="center">
<% 
   out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>

Now you can try to use the above JSP file to send an email with an HTML message.


Include attachments in the message

This example tells us how to send an email with an attachment.

<%@ 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
   String to = "abcd@gmail.com";

   // Sender's email
   String from = "mcmohd@gmail.com";

   // Suppose you are sending an email from a local host
   String host = "localhost";

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

   // Setting 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);

      // Setting From: Header field
      message.setFrom(new InternetAddress(from));

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

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

      // Create a message section
      BodyPart messageBodyPart = new MimeBodyPart();

      // Fill message
      messageBodyPart.setText("This is message body");
      
      // Create a multimedia message
      Multipart multipart = new MimeMultipart();

      // Set the text message section
      multipart.addBodyPart(messageBodyPart);

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

      // Send full message
      message.setContent(multipart );

      // Send a message
      Transport.send(message);
      String title = "Send Email";
      result = "Sent message successfully....";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message....";
   }
%>
<html>
<head>
<title>Send Attachement Email using JSP</title>
</head>
<body>
<center>
<h1>Send Attachement Email using JSP</h1>
</center>
<p align="center">
<% 
   out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>

User Authentication section

If the mail server requires a username and password for user authentication, it can be set up like this:

 properties.setProperty("mail.user", "myuser");
 properties.setProperty("mail.password", "mypwd");

Send a message using a form

Receive an email using an HTML form and get all email information via the request object:

String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
String messageText = request.getParameter("body");

Once you have the above information, you can use the example mentioned above to send the message.