
- JSP 基礎教程
- JSP - 首頁
- JSP - 概述
- JSP - 環境設定
- JSP - 架構
- JSP - 生命週期
- JSP - 語法
- JSP - 指令
- JSP - 動作
- JSP - 隱式物件
- JSP - 客戶端請求
- JSP - 伺服器響應
- JSP - HTTP 狀態碼
- JSP - 表單處理
- JSP - 編寫過濾器
- JSP - Cookie 處理
- JSP - 會話跟蹤
- JSP - 檔案上傳
- JSP - 處理日期
- JSP - 頁面重定向
- JSP - 點選計數器
- JSP - 自動重新整理
- JSP - 傳送電子郵件
- 高階 JSP 教程
- JSP - 標準標籤庫
- JSP - 資料庫訪問
- JSP - XML 資料
- JSP - Java Bean
- JSP - 自定義標籤
- JSP - 表示式語言
- JSP - 異常處理
- JSP - 除錯
- JSP - 安全性
- JSP - 國際化
- JSP 有用資源
- JSP - 問答
- JSP - 快速指南
- JSP - 有用資源
- JSP - 討論
JSP - 傳送電子郵件
在本章中,我們將討論如何使用 JSP 傳送電子郵件。要使用 JSP 傳送電子郵件,您應該在您的機器上安裝 **JavaMail API** 和 **Java Activation Framework (JAF)**。
您可以從 Java 的標準網站下載最新版本的 JavaMail (版本 1.2)。
您可以從 Java 的標準網站下載最新版本的 JavaBeans Activation Framework JAF (版本 1.0.2)。
下載並解壓縮這些檔案,到新建立的頂級目錄中。您將在兩個應用程式中找到許多 jar 檔案。您需要將 **mail.jar** 和 **activation.jar** 檔案新增到您的 CLASSPATH 中。
傳送簡單的電子郵件
這是一個從您的機器傳送簡單電子郵件的示例。假設您的 **localhost** 連線到 Internet 並且能夠傳送電子郵件。確保 Java 電子郵件 API 包和 JAF 包中的所有 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; // Recipient's email ID needs to be mentioned. String to = "abcd@gmail.com"; // Sender's email ID needs to be mentioned String from = "mcmohd@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties object Properties properties = System.getProperties(); // Setup 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 of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the 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>
現在讓我們將上述程式碼放入 **SendEmail.jsp** 檔案中,並使用 URL **https://:8080/SendEmail.jsp** 呼叫此 JSP。這將幫助您向給定的電子郵件 ID **abcd@gmail.com** 傳送電子郵件。您將收到以下響應 -
Send Email using JSP
Result: Sent message successfully....
如果要向多個收件人傳送電子郵件,請使用以下方法指定多個電子郵件 ID -
void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException
以下是引數的描述 -
**type** - 這將設定為 TO、CC 或 BCC。其中 CC 代表抄送,BCC 代表密送。例如 Message.RecipientType.TO
**addresses** - 這是電子郵件 ID 的陣列。在指定電子郵件 ID 時,您需要使用 InternetAddress() 方法
傳送 HTML 電子郵件
這是一個從您的機器傳送 HTML 電子郵件的示例。假設您的 **localhost** 連線到 Internet 並且能夠傳送電子郵件。確保 Java 電子郵件 API 包和 JAF 包中的所有 jar 檔案都可以在 CLASSPATH 中找到。
此示例與上一個示例非常相似,只是這裡我們使用 **setContent()** 方法來設定內容,其第二個引數是 **"text/html"**,以指定 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; // Recipient's email ID needs to be mentioned. String to = "abcd@gmail.com"; // Sender's email ID needs to be mentioned String from = "mcmohd@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties object Properties properties = System.getProperties(); // Setup 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 of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Subject Line!"); // Send the actual HTML message, as big as you like 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>
現在讓我們使用上面的 JSP 向給定的電子郵件 ID 傳送 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; // Recipient's email ID needs to be mentioned. String to = "abcd@gmail.com"; // Sender's email ID needs to be mentioned String from = "mcmohd@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties object Properties properties = System.getProperties(); // Setup 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 of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Subject Line!"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.setText("This is message body"); // Create a multipart message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Part two is attachment 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 parts 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 Attachment Email using JSP</h1> </center> <p align = "center"> <%out.println("Result: " + result + "\n");%> </p> </body> </html>
現在讓我們執行上面的 JSP,將檔案作為附件連同訊息一起傳送到給定的電子郵件 ID。
使用者身份驗證部分
如果需要向電子郵件伺服器提供使用者 ID 和密碼以進行身份驗證,則可以按如下方式設定這些屬性 -
props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd");
其餘的電子郵件傳送機制將與上面解釋的相同。
使用表單傳送電子郵件
您可以使用 HTML 表單接受電子郵件引數,然後可以使用 **request** 物件獲取所有資訊,如下所示 -
String to = request.getParameter("to"); String from = request.getParameter("from"); String subject = request.getParameter("subject"); String messageText = request.getParameter("body");
獲取所有資訊後,您可以使用上面提到的程式傳送電子郵件。