- ·上一篇文章:JSP 自动刷新
- ·下一篇文章:JSP 标准标签库(JSTL)
当前位置:K88软件开发 → 文章中心 → 编程语言 → JavaScript → JS02 → 文章内容
JSP 发送邮件
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 = "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 JSPResult:
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 = "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
头部的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 JSPResult:
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 = "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
JSP 发送邮件