- Java EE Tutorials
- JSP Tutorials
- Recommended Books for Java Server Pages (JSP)
Java Mail API needs to be used to send mail using JSP. Java Mail API is used to compose, read and write messages.You need to include jars javax.mail and javax.mail.activation to send email using JSP. These packages contain core classes of Java Mail API. Java Mail API provides classes which constitute a mail system. Java Mail is used to receive email via SMTP, POP3 and IMAP protocols.
- SMTP Protocol: It stands for Simple Mail Transfer Protocol which is used for transmission of mail across Internet Protocol (IP).It is also used for sending mail between servers. It can send messages to one server to another using POP or IMAP.
- POP Protocol: It stands for Post Office Protocol. It is used to retrieve mail from remote server. There are two types in POP. The First one is called POP2 which can be used to send mail using SMTP protocol. The second one is called POP3 which can be used to send mail with or without SMTP protocol.
- IMAP Protocol: It stands for Internet Message Access Protocol. Like POP protocol it is also used to retrieve mail from remote server.
Following steps are used to send mail using Java Mail API.
- Get the Session Object: To send email Java Mail API uses send () method on Transport class. This can be achieved by using JAVAMail Session object.
- Compose the message: Here we can write contents of message in body .The setSubject () method is used to set subject line and the setText () method is used to set content of the body. The javax.mail. Message package is used to compose the message.
- Send the message: We can send the message by using package javax.mail. Transport.
Now let us write a simple program to send an email. Write a JSP called sendemail.jsp. This file contains the logic to send email. Include jars activation.jar and javax.mail.jar in the WEB-INF/lib folder.
To send email you need to have a SMTP server that is responsible foe sending emails. Using JangoSMPT server (online) is one of the methods to get SMTP server. Other method may be install a SMPT server like postfix on your machine or use SMTP server provided by gmail,yahoo etc.
Via JangoSMPT server server emails are sent to our destination email address. You can create an account by visiting this site and configure your email adress.
Listing 1: sendemail.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 = "[email protected]"; // Sender's email ID needs to be mentioned String from = "[email protected]"; final String username = "someusername"; final String password = "password"; // Assuming you are sending email through relay.jangosmtp.net String host = "relay.jangosmtp.net"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "25"); //Get the Session object. Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // Create a default MimeMessage object. Message message = new MimeMessage(mailSession); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set Subject: header field message.setSubject("Testing Subject"); // Now set the actual message message.setText("Hello, this is sample for to check send " + "email using JavaMailAPI in JSP "); // Send message Transport.send(message); System.out.println("Sent message successfully...."); result = "Sent message successfully...."; } catch (MessagingException e) { e.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>
Execute the jsp file sendemail.jsp in Eclipse by selecting Run As > Run On Server an output as below would be seen:
If you check the inbox of “[email protected]”, you can see a new message with the text:
Hello, this is sample for to check send email using JavaMailAPI in JSP
Previous Tutorial : Upload File using JSP || Next Tutorial : JSP Expression Language