JSP Sending Email
Although it is simple to implement email sending using JSP, you need the JavaMail API and must install the JavaBean Activation Framework.
- You can download the latest version from the Java website at JavaMail; on the right side of the web page there is a Downloads link; click it to download.
- You can download the latest version from the Java website at JAF (version 1.1.1)。
You can also use the download links provided by this site:
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.
Sending 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 is able to send an email. Meanwhile, 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 = "abcd@gmail.com";
// 发件人的电子邮件
String from = "mcmohd@gmail.com";
// 假设你是从本地主机发送电子邮件
String host = "localhost";
// 获取系统属性对象
Properties properties = System.getProperties();
// Set mail server
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");
// Send message
Transport.send(message);
result = "Sent message successfully....";
}catch (MessagingException mex) {
mex.printStackTrace();
result = "Error: unable to send message....";
}
%>
Send Email using JSP
Send Email using JSP
<%
out.println("Result: " + result + "\n");
%>
Now visit http://localhost:8080/SendEmail.jsp; it will send an email to abcd@gmail.com and display the following result:
Send Email using JSP
Result: 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 MessagingException
The parameters are described 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; the InternetAddress() method is needed when specifying email addresses.
Sending an HTML Email
This example sends a simple HTML email. It assumes that your localhost is connected to the network and is able to send email. Meanwhile, 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 here we use the setContent() method and pass "text/html" as its 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;
// 收件人的电子邮件
String to = "abcd@gmail.com";
// 发件人的电子邮件
String from = "mcmohd@gmail.com";
// 假设你是从本地主机发送电子邮件
String host = "localhost";
// 获取系统属性对象
Properties properties = System.getProperties();
// Set mail server
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("This is actual message
",
"text/html" );
// Send message
Transport.send(message);
result = "Sent message successfully....";
}catch (MessagingException mex) {
mex.printStackTrace();
result = "Error: unable to send message....";
}
%>
Send HTML Email using JSP
Send Email using JSP
<%
out.println("Result: " + result + "\n");
%>
Now you can try using the above JSP file to send an email with an HTML message.
Including Attachments in 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;
// 收件人的电子邮件
String to = "abcd@gmail.com";
// 发件人的电子邮件
String from = "mcmohd@gmail.com";
// 假设你是从本地主机发送电子邮件
String host = "localhost";
// 获取系统属性对象
Properties properties = System.getProperties();
// Set mail server
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!");
// Create message part
BodyPart messageBodyPart = new MimeBodyPart();
// 填充消息
messageBodyPart.setText("This is message body");
// 创建多媒体消息
Multipart multipart = new MimeMultipart();
// Set text message part
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 );
// Send message
Transport.send(message);
String title = "Send Email";
result = "Sent message successfully....";
}catch (MessagingException mex) {
mex.printStackTrace();
result = "Error: unable to send message....";
}
%>
Send Attachement Email using JSP
Send Attachement Email using JSP
<%
out.println("Result: " + result + "\n");
%>
User authentication section
If the mail server requires a username and password for user authentication, you can configure it as follows:
properties.setProperty("mail.user", "myuser");
properties.setProperty("mail.password", "mypwd");
Sending 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 examples mentioned earlier to send the email.