JSP 发送邮件
jsp 发送邮件
虽然使用jsp实现邮件发送功能很简单,但是需要有javamail api,并且需要安装javabean activation framework。
- 您可以从 java 网站下载最新版本的 javamail,打开网页右侧有个 downloads 链接,点击它下载。
- 您可以从 java 网站下载最新版本的 jaf(版本 1.1.1)。
你也可以使用本站提供的下载链接:
下载并解压这些文件,在根目录下,您将会看到一系列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 = "abcd@gmail.com";
// 发件人的电子邮件
string from = "mcmohd@gmail.com";
// 假设你是从本地主机发送电子邮件
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,它将会发送一封邮件给abcd@gmail.com 并显示如下结果:
send email using jsp result: sent message successfully....
如果想要把邮件发送给多人,下面列出的方法可以用来指明多个邮箱地址:
void addrecipients(message.recipienttype type,
address[] addresses)
throws messagingexception
参数的描述如下:
- type:这个值将会被设置成 to(收件人)、cc 或 bcc。cc 表示 carbon copy(抄送),bcc 表示 blind carbon copy(密件抄送)。例子程序中使用的是 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 = "abcd@gmail.com";
// 发件人的电子邮件
string from = "mcmohd@gmail.com";
// 假设你是从本地主机发送电子邮件
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 = "abcd@gmail.com";
// 发件人的电子邮件
string from = "mcmohd@gmail.com";
// 假设你是从本地主机发送电子邮件
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>
用户认证部分
如果邮件服务器需要用户名和密码来进行用户认证的话,可以像下面这样来设置:
properties.setproperty("mail.user", "myuser");
properties.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");
获取以上信息后,您就可以使用前面提到的例子来发送邮件了。


