大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

JSP教程
JSP高級教程

JSP發送郵件

雖然使用JSP實現郵件發送功能很簡單,但是需要有JavaMail API,并且需要安裝JavaBean Activation Framework。

● 在這里下載最新版本的 JavaMail。

● 在這里下載最新版本的 JavaBeans Activation Framework(JAF)。

下載并解壓這些文件,在根目錄下,您將會看到一系列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....

如果想要把郵件發送給多人,下面列出的方法可以用來指明多個郵箱地址:

參數的描述如下:

● 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);       
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>

用戶認證部分

如果郵件服務器需要用戶名和密碼來進行用戶認證的話,可以像下面這樣來設置:

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");

獲取以上信息后,您就可以使用前面提到的例子來發送郵件了。

全部教程
主站蜘蛛池模板: 国产精品亚洲精品爽爽 | 97在线视频免费播放 | 国产二区在线播放 | 久久久噜噜噜久噜久久 | 国产香蕉视频在线播放 | 天天干在线观看 | 国语一区 | 欧美日韩视频一区三区二区 | 免费观看一级毛片 | 久草热久草在线 | 久久国产片 | 国产精品亚洲一区二区三区久久 | www成人在线观看 | 亚洲欧美视频一区二区三区 | 神马影院我不卡888 神马影院我不卡手机 | 男女超爽视频免费播放在线观看 | 日韩欧美在线播放 | 久久人人爽人人爽 | 中文字幕一区二区三区视频在线 | 色综合久久综合网 | 欧美日本黄色 | 精品久久久久久久久免费影院 | 伊人婷婷色香五月综合缴缴情 | 四虎永久免费紧急入口 | 99热最新网址获取 | 在线中文字幕一区 | 人人草人人干 | 亚洲第一在线 | 精品国产91在线网 | 国产综合图区 | 免费在线观看的毛片 | 亚洲欧美在线中文字幕不卡 | 高清视频 一区二区三区四区 | 亚洲免费视频在线观看 | 日韩国产欧美一区二区三区 | 成人欧美一区在线视频在线观看 | 四虎海外影库www4hu | 日本操操操 | 四虎在线精品免费高清在线 | 99久久做夜夜爱天天做精品 | 日日夜夜亚洲 |