Although it is very simple to use JSP to send emails, it requires the JavaMail API and the JavaBean Activation Framework to be installed.
Download the latest version of JavaMail here.
Download the latest version of JavaBeans Activation Framework (JAF) here.
Download and unzip these files. In the root directory, you will see a series of jar packages. Add the mail.jar package and activation.jar package to the CLASSPATH variable.
This example shows how to send a simple email from your machine. It assumes that localhost is connected to the network and has the ability to send an email. At the same time, please confirm 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 = "[email protected]"; // Send Sender's email String from = "[email protected]"; // Assume you are sending email from local host String host = "localhost"; // Get the system properties object Properties properties = System.getProperties(); // Set the 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 the header field of the From: header message.setFrom(new InternetAddress(from)); // Set the header field of the To: header message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Subject Line!"); // Now set the actual message message.setText("This is actual message"); // Send 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>
Now visit http://localhost:8080/SendEmail.jsp, it will send an email to [email protected] and display the following results:
Send Email using JSPResult: Sent message successfully....If you want to send an email to multiple people, the methods listed below can be used to specify multiple email addresses:
void addRecipients(Message.RecipientType type, Address[] addresses)throws MessagingExceptionThe parameters are described as follows:
type: This value will be set to TO, CC, or BCC. CC stands for copy, BCC stands for black copy, and TO is used in the example program.
addresses: This is an array of email addresses. When specifying an email address, you need to use the InternetAddress() method.
This example sends a simple HTML email. It assumes that your localhost is connected to the network and has the ability to send mail. At the same time, please confirm 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 use the setContent() method and pass "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 = "[email protected]"; // Send Sender's email String from = "[email protected]"; // Assume you are sending email from local host String host = "localhost"; // Get the system properties object Properties properties = System.getProperties(); // Set the 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 the header field of the From: header message.setFrom(new InternetAddress(from)); // Set the header field of the To: header message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Subject Line!"); // Set 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><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 HTML message to email.
This example shows us how to send an email containing 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 = "[email protected]"; // Send Sender's email String from = "[email protected]"; // Assume you are sending email from local host String host = "localhost"; // Get the system properties object Properties properties = System.getProperties(); // Set the 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 the header field of the From: header message.setFrom(new InternetAddress(from)); // Set the header field of the To: header message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); //Set the Subject: header field message.setSubject("This is the Subject Line!"); //Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Fill in the message messageBodyPart.setText("This is message body"); // Create a multimedia message Multipart multipart = new MimeMultipart(); // Set the text message part multipart.addBodyPart(messageBodyPart); / / Attachment part 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 message.setContent(multipart ); // Send the 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>
If the mail server requires a username and password for user authentication, it can be set as follows:
props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd");Use an HTML form to receive an email and obtain all email information through 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 send the email using the example mentioned earlier.