JSP-Runoob:JSP 發送郵件
阿新 • • 發佈:2018-09-05
import tty row request對象 內容 pass work ESS uno
1、
ylbtech-JSP-Runoob:JSP 發送郵件 |
1.返回頂部 |
JSP 發送郵件
雖然使用JSP實現郵件發送功能很簡單,但是需要有JavaMail API,並且需要安裝JavaBean Activation Framework。
- 您可以從 Java 網站下載最新版本的 JavaMail,打開網頁右側有個 Downloads 鏈接,點擊它下載。
- 您可以從 Java 網站下載最新版本的 JAF(版本 1.1.1)。
你也可以使用本站提供的下載鏈接:
- JavaMail mail.jar 1.4.5
- JAF(版本 1.1.1) activation.jar
下載並解壓這些文件,在根目錄下,您將會看到一系列jar包。將mail.jar包和activation.jar包加入CLASSPATH變量中。
發送一封簡單的郵件
這個例子展示了如何從您的機器發送一封簡單的郵件。它假定localhost已經連接至網絡並且有能力發送一封郵件。與此同時,請再一次確認mail.jar包和activation.jar包已經添加進CLASSPATH變量中。
<%@ 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>
現在訪問http://localhost:8080/SendEmail.jsp,它將會發送一封郵件給[email protected] 並顯示如下結果:
Send Email using JSP
Result: Sent message successfully....
如果想要把郵件發送給多人,下面列出的方法可以用來指明多個郵箱地址:
void addRecipients(Message.RecipientType type,
Address[] addresses)
throws MessagingException
參數的描述如下:
- type:這個值將會被設置成TO,CC,或BCC。CC代表副本,BCC代表黑色副本,例子程序中使用的是TO。
- addresses:這是一個郵箱地址的數組,當指定郵箱地址時需要使用InternetAddress()方法。
發送一封HTML郵件
這個例子發送一封簡單的HTML郵件。它假定您的localhost已經連接至網絡並且有能力發送郵件。與此同時,請再一次確認mail.jar包和activation.jar包已經添加進CLASSPATH變量中。
這個例子和前一個例子非常相似,不過在這個例子中我們使用了setContent()方法,將"text/html"做為第二個參數傳給它,用來表明消息中包含了HTML內容。
<%@ 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>
現在你可以嘗試使用以上JSP文件來發送HTML消息的電子郵件。
在郵件中包含附件
這個例子告訴我們如何發送一封包含附件的郵件。
<%@ 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);