JSP Sending Email
Although using JSP to implement email sending functionality is simple, it requires the JavaMail API and the JavaBean Activation Framework.
- You can download the latest version from the Java websiteJavaMail, there is aDownloadslink on the right side of the webpage, click it to download.
- You can download the latest version from the Java websiteJAF (version 1.1.1)。
You can also use the download link provided by this site:
- JavaMail mail.jar 1.4.5
- JAF (version 1.1.1) activation.jar
Download and extract these files. In the root directory, you will see a series of jar packages. Add the mail.jar and activation.jar packages 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 already connected to the network and is capable of sending an email. At the same time, please confirm once again that the mail.jar and activation.jar packages 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; // 收件人的电子邮件 String to = "[email protected]"; // 发件人的电子邮件 String from = "[email protected]"; // 假设你是从本地主机发送电子邮件 String host = "localhost"; // 获取系统属性对象 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); // 获取默认的Session对象。 Session mailSession = Session.getDefaultInstance(properties); try{ // 创建一个默认的MimeMessage对象。 MimeMessage message = new MimeMessage(mailSession); // 设置 From: 头部的header字段 message.setFrom(new InternetAddress(from)); // 设置 To: 头部的header字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置 Subject: header字段 message.setSubject("This is the Subject Line!"); // 现在设置的实际消息 message.setText("This is actual 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 result:
Send Email using JSP Result: Sent message successfully....
If you want to send the 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 parameter descriptions are as follows:
- type: This value will be set to TO (recipient), CC, or BCC. CC stands for Carbon Copy, BCC stands for Blind Carbon Copy. The example program uses TO.
- addresses: This is an array of email addresses. When specifying email addresses, you need to use the InternetAddress() method.
Send an HTML email
This example sends a simple HTML email. It assumes that your localhost is already connected to the network and is capable of sending email. At the same time, please confirm once again that the mail.jar and activation.jar packages have been added to the CLASSPATH variable.
This example is very similar to the previous one, except that in this example we use the setContent() method, passing "text/html" as the second parameter to it, 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; // 收件人的电子邮件 String to = "[email protected]"; // 发件人的电子邮件 String from = "[email protected]"; // 假设你是从本地主机发送电子邮件 String host = "localhost"; // 获取系统属性对象 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); // 获取默认的Session对象。 Session mailSession = Session.getDefaultInstance(properties); try{ // 创建一个默认的MimeMessage对象。 MimeMessage message = new MimeMessage(mailSession); // 设置 From: 头部的header字段 message.setFrom(new InternetAddress(from)); // 设置 To: 头部的header字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置 Subject: header字段 message.setSubject("This is the Subject Line!"); // 设置 HTML消息 message.setContent("<h1>This is actual message</h1>", "text/html" ); // 发送消息 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 email with an HTML message.
Include attachments in the email
This example tells 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; // 收件人的电子邮件 String to = "[email protected]"; // 发件人的电子邮件 String from = "[email protected]"; // 假设你是从本地主机发送电子邮件 String host = "localhost"; // 获取系统属性对象 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); // 获取默认的Session对象。 Session mailSession = Session.getDefaultInstance(properties); try{ // 创建一个默认的MimeMessage对象。 MimeMessage message = new MimeMessage(mailSession); // 设置 From: 头部的header字段 message.setFrom(new InternetAddress(from)); // 设置 To: 头部的header字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置 Subject: header字段 message.setSubject("This is the Subject Line!"); // 创建消息部分 BodyPart messageBodyPart = new MimeBodyPart(); // 填充消息 messageBodyPart.setText("This is message body"); // 创建多媒体消息 Multipart multipart = new MimeMultipart(); // 设置文本消息部分 multipart.addBodyPart(messageBodyPart); // 附件部分 messageBodyPart = new MimeBodyPart(); String filename = "file.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // 发送完整消息 message.setContent(multipart ); // 发送消息 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 part
If the mail server requires a username and password for user authentication, you can set it up as follows:
properties.setProperty("mail.user", "myuser");
properties.setProperty("mail.password", "mypwd");
Send email using a form
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");
After obtaining the above information, you can use the previously mentioned examples to send the email.
Other extensions