English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Although it is very simple to implement email sending functionality using JSP, it requires the JavaMail API and the installation of JavaBean Activation Framework.
You can download the latest version from the Java website JavaMail, there is a Downloads link, click it to download.
You can download the latest version from the Java website JAF (version 1.1.1).
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.
This example demonstrates 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 the 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; // The recipient's email String to = "[email protected]"; // The sender's email String from = "[email protected]"; // Assuming you are sending an email from the 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 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 the Subject Line!"); // The actual message set now 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>/title> </head> <body> <center> <h1>Send Email using JSP</h1> </center> <p align="center"> <% out.println("Result: " + result + "\n"); %> </p> </body> </html>
Now access http://localhost:8080/SendEmail.jsp, it will send an email to [email protected] and display the following result:
Send Email using JSP Result: Sent message successfully....
If you want to send an email to multiple people, the following methods 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 (cc), and BCC stands for Blind Carbon Copy (blind cc). The TO is used in the example program.
addresses: This is an array of email addresses, and the InternetAddress() method needs to be used when specifying an email address.
This example sends a simple HTML email. It assumes that your localhost is connected to the network and capable of sending emails. At the same time, please confirm once again that the mail.jar package and the activation.jar package have been added to the CLASSPATH variable.
This example is very similar to the previous one, but in this example, we use 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; // The recipient's email String to = "[email protected]"; // The sender's email String from = "[email protected]"; // Assuming you are sending an email from the 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 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 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 using the above JSP file to send an HTML message email.
This example shows 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; // The recipient's email String to = "[email protected]"; // The sender's email String from = "[email protected]"; // Assuming you are sending an email from the 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 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 the Subject Line!"); // Create message part BodyPart messageBodyPart = new MimeBodyPart(); // Fill message messageBodyPart.setText("This is message body"); // Create multimedia message Multipart multipart = new MimeMultipart(); // Set 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 complete message 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...."; } %> <html> <head> <title>Send Attachment 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 email server requires username and password for user authentication, you can set it as follows:
properties.setProperty("mail.user", "myuser"); properties.setProperty("mail.password", "mypwd");
Receive an email using an HTML form and retrieve 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");
After obtaining the above information, you can use the examples mentioned earlier to send emails.