新聞中心
雖然使用JSP實現(xiàn)郵件發(fā)送功能很簡單,但是需要有JavaMail API,并且需要安裝JavaBean Activation Framework。

成都創(chuàng)新互聯(lián)公司服務項目包括寧波網(wǎng)站建設、寧波網(wǎng)站制作、寧波網(wǎng)頁制作以及寧波網(wǎng)絡營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯(lián)網(wǎng)行業(yè)的解決方案,寧波網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到寧波省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!
- 在這里下載最新版本的 JavaMail。
- 在這里下載最新版本的 JavaBeans Activation Framework(JAF)。
下載并解壓這些文件,在根目錄下,您將會看到一系列jar包。將mail.jar包和activation.jar包加入CLASSPATH變量中。
發(fā)送一封簡單的郵件
這個例子展示了如何從您的機器發(fā)送一封簡單的郵件。它假定localhost已經(jīng)連接至網(wǎng)絡并且有能力發(fā)送一封郵件。與此同時,請再一次確認mail.jar包和activation.jar包已經(jīng)添加進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]";
// 發(fā)件人的電子郵件
String from = "[email protected]";
// 假設你是從本地主機發(fā)送電子郵件
String host = "localhost";
// 獲取系統(tǒng)屬性對象
Properties properties = System.getProperties();
// 設置郵件服務器
properties.setProperty("mail.smtp.host", host);
// 獲取默認的Session對象。
Session mailSession = Session.getDefaultInstance(properties);
try{
// 創(chuàng)建一個默認的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!");
// 現(xiàn)在設置的實際消息
message.setText("This is actual message");
// 發(fā)送消息 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"); %>
現(xiàn)在訪問http://localhost:8080/SendEmail.jsp,它將會發(fā)送一封郵件給[email protected] 并顯示如下結果:
Send Email using JSP Result: Sent message successfully....
如果想要把郵件發(fā)送給多人,下面列出的方法可以用來指明多個郵箱地址:
void addRecipients(Message.RecipientType type,
Address[] addresses)
throws MessagingException
參數(shù)的描述如下:
- type:這個值將會被設置成TO,CC,或BCC。CC代表副本,BCC代表黑色副本,例子程序中使用的是TO。
- addresses:這是一個郵箱地址的數(shù)組,當指定郵箱地址時需要使用InternetAddress()方法。
發(fā)送一封HTML郵件
這個例子發(fā)送一封簡單的HTML郵件。它假定您的localhost已經(jīng)連接至網(wǎng)絡并且有能力發(fā)送郵件。與此同時,請再一次確認mail.jar包和activation.jar包已經(jīng)添加進CLASSPATH變量中。
這個例子和前一個例子非常相似,不過在這個例子中我們使用了setContent()方法,將"text/html"做為第二個參數(shù)傳給它,用來表明消息中包含了HTML內(nèi)容。
<%@ 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]";
// 發(fā)件人的電子郵件
String from = "[email protected]";
// 假設你是從本地主機發(fā)送電子郵件
String host = "localhost";
// 獲取系統(tǒng)屬性對象
Properties properties = System.getProperties();
// 設置郵件服務器
properties.setProperty("mail.smtp.host", host);
// 獲取默認的Session對象。
Session mailSession = Session.getDefaultInstance(properties);
try{ // 創(chuàng)建一個默認的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" );
// 發(fā)送消息
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"); %>
現(xiàn)在你可以嘗試使用以上JSP文件來發(fā)送HTML消息的電子郵件。
在郵件中包含附件
這個例子告訴我們?nèi)绾伟l(fā)送一封包含附件的郵件。
<%@ 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]";
// 發(fā)件人的電子郵件
String from = "[email protected]";
// 假設你是從本地主機發(fā)送電子郵件
String host = "localhost";
// 獲取系統(tǒng)屬性對象
Properties properties = System.getProperties();
// 設置郵件服務器
properties.setProperty("mail.smtp.host", host);
// 獲取默認的Session對象。
Session mailSession = Session.getDefaultInstance(properties);
try{
// 創(chuàng)建一個默認的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!");
// 創(chuàng)建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 填充消息
messageBodyPart.setText("This is message body");
// 創(chuàng)建多媒體消息
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);
// 發(fā)送完整消息
message.setContent(multipart );
// 發(fā)送消息
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"); %>
用戶認證部分
如果郵件服務器需要用戶名和密碼來進行用戶認證的話,可以像下面這樣來設置:
props.setProperty("mail.user", "myuser");
props.setProperty("mail.password", "mypwd");
使用表單發(fā)送郵件
使用HTML表單接收一封郵件,并通過request對象獲取所有郵件信息:
String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
String messageText = request.getParameter("body");
獲取以上信息后,您就可以使用前面提到的例子來發(fā)送郵件了。
標題名稱:創(chuàng)新互聯(lián)JSP教程:JSP發(fā)送郵件
鏈接地址:http://m.fisionsoft.com.cn/article/dhpjohc.html


咨詢
建站咨詢
