以下内容引用自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发送电子邮件的更多相关文章

  1. JAVA实现发送电子邮件

    相信大家对于网站也好,手机app也好,用户注册时,需要进行邮箱验证的功能特别好奇吧,本篇我将带领大家一起实现一下这个简单而又神奇的小功能,让我们的应用也可以加入这些神奇的元素.废话不多说,下面开始我们 ...

  2. WordPress ”无法发送电子邮件,可能原因:您的主机禁用了mail()函数“的解决办法

    WordPress网站中出现 "无法发送电子邮件,可能原因:您的主机禁用了mail()函数"的情况一般都是因为所在主机环境不支持在线邮件收发功能导致,如果不支持的话,那么像类似 N ...

  3. javamail模拟邮箱功能发送电子邮件-基础实战篇(javamail API电子邮件实例)

    引言: JavaMail 是一种可选的.能用于读取.编写和发送电子消息的包 JavaMail jar包下载地址:http://java.sun.com/products/javamail/downlo ...

  4. javamail模拟邮箱功能发送电子邮件-中级实战篇【新增附件发送方法】(javamail API电子邮件实例)

    引言: JavaMail jar包下载地址:http://java.sun.com/products/javamail/downloads/index.html 此篇是紧随上篇文章而封装出来的,阅读本 ...

  5. 在ASP.NET中发送电子邮件的实例教程

    首先.导入命名空间: 代码如下 复制代码 using System.Net.Mail; 定义发送电子邮件的方法[网上很多不同的,可以对比着看一下,WinForm的也适用]: 代码如下 复制代码 /// ...

  6. ASP.NET 发送电子邮件简介

    1.补充知识 (1)POP3和SMTP服务器是什么? 简单点来说:POP3 用于接收电子邮件 ,SMTP 用于发送电子邮件. (1)POP3具体指什么? POP3(Post Office Protoc ...

  7. 使用php发送电子邮件(phpmailer)

    在项目开发过程中,经常会用到通过程序发送电子邮件,例如:注册用户通过邮件激活,通过邮件找回密码,发送报表等.这里介绍几种通过PHP发送电子邮件的 方式(1)通过mail()函数发送邮件(2)使用fso ...

  8. 利用Excel批量高速发送电子邮件

    利用Excel批量高速发送电子邮件,分两步: 1. 准备待发送的数据: a.) 打开Excel,新建Book1.xlsx b.) 填入以下的内容, 第一列:接收人,第二列:邮件标题,第三列:正文,第四 ...

  9. Spring发送电子邮件

      Spring提供了发送电子邮件的功能,它向用户屏蔽了底层邮件系统的一些细节,同时代表客户端负责底层的资源处理. Spring的邮件服务支持主要是通过JavaMailSender这个接口实现的: p ...

随机推荐

  1. Sql中创建事务处理

    Create Procedure MyProcedure AS Begin Set NOCOUNT ON; Set XACT_ABORT ON; --这句话非常重要 begin try Begin T ...

  2. Apache Tomcat 之路(二 部署web 应用程序)

    1.创建一个webapplication,不论是解压的应用程序包还是war包,在tomcat 上都能部署,这里提供一个简单的web项目:git地址:https://github.com/coderxi ...

  3. java实现的单点登录

    摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统当中.本文从业务的角度分析了单点登录的需求和应用领域:从技术本身的角度分析了单点登录技术的内部机制和实现手段,并且给出Web-SSO ...

  4. Farseer.net轻量级ORM开源框架 V1.x 入门篇:视图的数据操作

    导航 目   录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:视图实体类映射 下一篇:Farseer.net轻量级ORM开源 ...

  5. Ubuntu 几个常用的更新命令

    apt-cache search package 搜索包 apt-cache show package 获取包的相关信息,如说明.大小.版本等 sudo apt-get install package ...

  6. HDU_1237_简单计算器

    运算符为+,-,*,/:操作数为整数:且没有括号 设定符号优先级,先在栈底压运算符0 #include<iostream> #include<cstdio> #include& ...

  7. CentOS 初体验十四:阿里云安装Gitlab

    网址:https://about.gitlab.com/install/#centos-7 https://blog.csdn.net/zhaoyanjun6/article/details/7914 ...

  8. 怎么在在php配置文件中打开php_fileinfo扩展

    运行composer下载文件时的相关报错截图 处理方法: 在php配置文件中打开php_fileinfo扩展 你需要在查看是否有这个扩展包, 如果有, 那就在php.ini写 extension=ph ...

  9. 神经网络(NN)+反向传播算法(Backpropagation/BP)+交叉熵+softmax原理分析

    神经网络如何利用反向传播算法进行参数更新,加入交叉熵和softmax又会如何变化? 其中的数学原理分析:请点击这里.

  10. P1387 最大正方形&&P1736 创意吃鱼法

    P1387 最大正方形 P1736 创意吃鱼法 两道类似的$DP$ 转移方程基本上类似于$f[i][j]=min(f[i-1][j-1],min(f[i][j-1],f[i-1][j]))$ 考虑构成 ...