JSP发送电子邮件
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/sending-email.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!");
// 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:http://localhost:8080/SendEmail.jsp来调用此JSP,将电子邮件发送到给定的邮箱ID为abcd@gmail.com中,显示的响应结果如下所示。

如果想给多个收件人发送邮件,下面的方法可用于发送邮件给指定的多个邮箱IDs:
void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException
下面是对参数的描述:
type:可设置为TO,CC或者BCC。其中,CC表示Carbon Copy,BCC表示Black Carbon Copy。例如,Message.RecipientType.TO。
- addresses:它是邮箱ID的数组。能使用InternetAddress()方法,同时指定邮箱IDs。
发送HTML邮件
这个例子与之前给出的例子非常相似,除了在这个例子中使用setContent()方法设置第二个参数为“text/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。
在电子邮件中发送附件:
给定一个例子,从机器上发送一个带附件的邮箱:
<%@ 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 multipar 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 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>
现在可以运行上述JSP发送一个文件作为信息的附件给指定邮箱ID。
用户身份验证部分
为了身份验证的目的,如果需要提供用户ID和密码给邮箱服务器,可以设置这些属性如下:
props.setProperty("mail.user", "myuser");
props.setProperty("mail.password", "mypwd");
邮件发送机制的设置和上述的解释一致。
用表单发送邮件
可以使用网页表单接受邮件参数,然后可以使用Request对象获得如下的所有信息:
String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
String messageText = request.getParameter("body");
测试工程:https://github.com/easonjim/5_java_example/tree/master/jspbasics/test16
JSP发送电子邮件的更多相关文章
- JAVA实现发送电子邮件
相信大家对于网站也好,手机app也好,用户注册时,需要进行邮箱验证的功能特别好奇吧,本篇我将带领大家一起实现一下这个简单而又神奇的小功能,让我们的应用也可以加入这些神奇的元素.废话不多说,下面开始我们 ...
- WordPress ”无法发送电子邮件,可能原因:您的主机禁用了mail()函数“的解决办法
WordPress网站中出现 "无法发送电子邮件,可能原因:您的主机禁用了mail()函数"的情况一般都是因为所在主机环境不支持在线邮件收发功能导致,如果不支持的话,那么像类似 N ...
- javamail模拟邮箱功能发送电子邮件-基础实战篇(javamail API电子邮件实例)
引言: JavaMail 是一种可选的.能用于读取.编写和发送电子消息的包 JavaMail jar包下载地址:http://java.sun.com/products/javamail/downlo ...
- javamail模拟邮箱功能发送电子邮件-中级实战篇【新增附件发送方法】(javamail API电子邮件实例)
引言: JavaMail jar包下载地址:http://java.sun.com/products/javamail/downloads/index.html 此篇是紧随上篇文章而封装出来的,阅读本 ...
- 在ASP.NET中发送电子邮件的实例教程
首先.导入命名空间: 代码如下 复制代码 using System.Net.Mail; 定义发送电子邮件的方法[网上很多不同的,可以对比着看一下,WinForm的也适用]: 代码如下 复制代码 /// ...
- ASP.NET 发送电子邮件简介
1.补充知识 (1)POP3和SMTP服务器是什么? 简单点来说:POP3 用于接收电子邮件 ,SMTP 用于发送电子邮件. (1)POP3具体指什么? POP3(Post Office Protoc ...
- 使用php发送电子邮件(phpmailer)
在项目开发过程中,经常会用到通过程序发送电子邮件,例如:注册用户通过邮件激活,通过邮件找回密码,发送报表等.这里介绍几种通过PHP发送电子邮件的 方式(1)通过mail()函数发送邮件(2)使用fso ...
- 利用Excel批量高速发送电子邮件
利用Excel批量高速发送电子邮件,分两步: 1. 准备待发送的数据: a.) 打开Excel,新建Book1.xlsx b.) 填入以下的内容, 第一列:接收人,第二列:邮件标题,第三列:正文,第四 ...
- Spring发送电子邮件
Spring提供了发送电子邮件的功能,它向用户屏蔽了底层邮件系统的一些细节,同时代表客户端负责底层的资源处理. Spring的邮件服务支持主要是通过JavaMailSender这个接口实现的: p ...
随机推荐
- 30款jQuery常用网页焦点图banner图片切换
1.jquery 图片滚动特效制作 slide 图片类似窗帘式图片滚动 查看演示 2.jquery幻灯片插件带滚动条的圆形立体图片旋转滚动 查看演示 3.jQuery图片层叠旋转类似洗牌翻转图片幻灯片 ...
- R in action读书笔记(8)-第八章:回归(上)
8.1回归的多面性 8.2 OLS回归 OLS回归拟合模型形式: 为了能够恰当地解释oLs模型的系数,数据必须满足以下统计假设. 口正态性对于固定的自变量值,因变量值成正态分布. 口独立性Yi值之间相 ...
- [翻译] API测试最佳实践 - 身份验证(Authentication)
API测试最佳实践 - 身份验证 适用等级:高级 1. 概况 身份验证通常被定义为是对某个资源的身份的确认的活动,这里面资源的身份指代的是API的消费者(或者说是调用者).一旦一个用户的身份验证通过了 ...
- 契约式设计(DbC)感想(二)
契约式设计6大原则的理解 在<Design by Contract原则与实践>中,作者定义了契约式设计的6大原则: 区分命令和查询: 将基本查询和派生查询区分开: 针对每个派生查询,设定一 ...
- python Matplotlib 系列教程(三)——绘制直方图和条形图
在本章节我们将学习如何绘制条形图和直方图 条形图与直方图的区别:首先,条形图是用条形的长度表示各类别频数的多少,其宽度(表示类别)则是固定的: 直方图是用面积表示各组频数的多少,矩形的高度表示每一组的 ...
- 显微镜下的webpack4入门
前端的构建打包工具很多,比如grunt,gulp.相信这两者大家应该是耳熟能详的,上手相对简单,而且所需手敲的代码都是比较简单的.然后webpack的出现,让这两者打包工具都有点失宠了.webpack ...
- 小甲鱼Python学习笔记
一 isdigit()True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字False: 汉字数字Error: 无 isdecimal()True: Unicode数字, ...
- 简述FTP主动模式与被动模式
1 FTP工作模式 2 不同模式FTP面临的问题 3 主动模式的FTP连接建立连接主要步骤 客户端打开一个随机的端口(端口号大于1024,在这里,我们称它为x),同时一个FTP进程连接至服务器的21号 ...
- Python之面向对象新式类和经典类
Python之面向对象新式类和经典类 新式类和经典类的继承原理: 在Python3中,就只有新式类一种了. 先看Python3中新式类: 类是有继承顺序的: Python的类是可以继承多个类的,也就是 ...
- sqlserver常用简单语句
1.增 插入内容 insert into <表名> (列1,列2,列3) values ('值1','值2','值3') 检索出的内容插入到另外一张表 insert into <表名 ...