Spring提供了发送电子邮件的功能,它向用户屏蔽了底层邮件系统的一些细节,同时代表客户端负责底层的资源处理。
Spring的邮件服务支持主要是通过JavaMailSender这个接口实现的:
public
interface
JavaMailSender
extends
MailSender {
MimeMessage createMimeMessage();
MimeMessage createMimeMessage(InputStream contentStream)
throws
MailException;
void
send(MimeMessage mimeMessage)
throws
MailException;
void
send(MimeMessage[] mimeMessages)
throws
MailException;
void
send(MimeMessagePreparator mimeMessagePreparator)
throws
MailException;
void
send(MimeMessagePreparator[] mimeMessagePreparators)
throws
MailException;
}
这是JavaMailSender的接口源代码(去除注释),可以看到,主要提供了createMimeMessage和send两个方法。createMimeMessage方法主要是用来创建JavaMail的MIME信件,而send则是发送电子邮件的主要方法。
Spring中提供了JavaMailSender的简单实现:org.springframework.mail.javamail.JavaMailSenderImpl。在JavaMailSendImpl这个类中,实现了JavaMailSender中定义的方法的具体实现。而众所周知,Spring是核心的功能是IOC,所以通过Spring来发送电子邮件,就可以使用Spring强大的IOC功能,下面就来看一下,怎么样在Spring中发送邮件:
1. Spring配置文件,主要配置mailSender和对velocity的支持
<?
xml version="1.0" encoding="UTF-8"
?>
<
beans
xmlns
="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-lazy-init
="true"
default-autowire
="byName"
>
<!--
属性文件加载, 加载邮件设置属性文件
-->
<
bean
id
="propertyConfigurer"
class
="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
>
<
property
name
="locations"
>
<
list
>
<
value
>
classpath:mail.properties
</
value
>
</
list
>
</
property
>
</
bean
>
<
bean
id
="mailEngine"
class
="org.example.mailer.MailEngine"
>
<
property
name
="javaMailSender"
ref
="javaMailSender"
/>
<
property
name
="velocityEngine"
ref
="velocityEngine"
/>
</
bean
>
<
bean
id
="velocityEngine"
class
="org.springframework.ui.velocity.VelocityEngineFactoryBean"
>
<
property
name
="resourceLoaderPath"
value
="classpath:velocity"
/>
</
bean
>
<!--
邮件发送器
-->
<
bean
id
="javaMailSender"
class
="org.springframework.mail.javamail.JavaMailSenderImpl"
>
<
property
name
="host"
value
="${mail.host}"
/>
<
property
name
="username"
value
="${mail.username}"
/>
<
property
name
="password"
value
="${mail.password}"
/>
<
property
name
="defaultEncoding"
value
="UTF-8"
></
property
>
<
property
name
="javaMailProperties"
>
<
props
>
<
prop
key
="mail.smtp.auth"
>
${mail.smtp.auth}
</
prop
>
<
prop
key
="mail.smtp.timeout"
>
${mail.smtp.timeout}
</
prop
>
</
props
>
</
property
>
</
bean
>
</
beans
>
在这个配置文件中,通过
propertyConfigurer这个bean
加载了邮件的配置文件:mail.properties,这个文件主要定义一些邮件服务的属性(使用的时候根据自己的要求进行相应的配置,这里以126的smtp服务为例):
mail.from
=
mail.host
=
smtp.
126
.com
mail.username
=
mail.password
=
mail.smtp.auth
=
true
mail.smtp.timeout
=
25000
下面来看一下MailEngine
的实现:
package
org.example.mailer;
import
java.util.Map;
import
javax.mail.MessagingException;
import
javax.mail.internet.MimeMessage;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
org.apache.velocity.app.VelocityEngine;
import
org.apache.velocity.exception.VelocityException;
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
org.springframework.mail.MailException;
import
org.springframework.mail.javamail.JavaMailSender;
import
org.springframework.mail.javamail.MimeMessageHelper;
import
org.springframework.ui.velocity.VelocityEngineUtils;
public
class
MailEngine {
protected
final
Log logger
=
LogFactory.getLog(getClass());
private
JavaMailSender javaMailSender;
private
VelocityEngine velocityEngine;
public
void
setJavaMailSender(JavaMailSender javaMailSender) {
this
.javaMailSender
=
javaMailSender;
}
public
void
setVelocityEngine(VelocityEngine velocityEngine) {
this
.velocityEngine
=
velocityEngine;
}
public
void
sendMailWithVelocity() {
MimeMessage msg
=
javaMailSender.createMimeMessage();
MimeMessageHelper helper
=
new
MimeMessageHelper(msg);
String result
=
null
;
Map model
=
null
;
try
{
result
=
VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine,
"
sendMail.vm
"
,
"
UTF-8
"
, model);
//
UTF-8为模板文件的字符编码
helper.setFrom(
"
邮件发送者
"
);
helper.setSubject(
"
测试Spring邮件
"
);
helper.setTo(
"
邮件接收者
"
);
helper.setText(result);
javaMailSender.send(msg);
}
catch
(VelocityException e) {
e.printStackTrace();
logger.error(e.getMessage());
}
catch
(MessagingException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
public
boolean
senaMail() {
MimeMessage msg
=
javaMailSender.createMimeMessage();
MimeMessageHelper helper
=
new
MimeMessageHelper(msg);
try
{
helper.setFrom(
"
邮件发送者
"
);
helper.setSubject(
"
邮件内容
"
);
helper.setTo(
"
邮件接收者
"
);
helper.setText(
"
test spring mailer
"
,
true
);
//
如果发的不是html内容去掉true参数
javaMailSender.send(msg);
}
catch
(MessagingException e) {
//
TODO 自动生成 catch 块
if
(logger.isWarnEnabled()) {
logger.warn(
"
邮件信息导常! 邮件标题为:
"
);
}
return
false
;
//
e.printStackTrace();
}
catch
(MailException me) {
//
TODO: handle exception
if
(logger.isWarnEnabled()) {
logger.warn(
"
发送邮件失败! 邮件标题为:
"
);
}
return
false
;
}
return
true
;
}
public
static
void
main(String[] args) {
BeanFactory bf
=
new
ClassPathXmlApplicationContext(
"
beans.xml
"
);
MailEngine mailEngine
=
(MailEngine) bf.getBean(
"
mailEngine
"
);
//
mailEngine.senaMail();
mailEngine.sendMailWithVelocity();
}
}
sendMailWithVelocity方法主要是使用Velocity模板文件来发送邮件,Velocity模板文件定义了邮件的内容,模板文件的位置由
resourceLoaderPath
指定,本例则在classpath下的velocity下,如果是web项目,则位于/WEB-INF/classes/veloticy/目录下。
执行main方法,就可以发送邮件了。
分享到:
相关推荐
它包括了发送电子邮件的主要接口MailSender,和值对象SimpleMailMessage,它封装了简单邮件的属性如from, to,cc, subject,text。 包里还包含一棵以MailException为根的checked Exception继承树,它们提供了对底层...
Spring提供了非常好用的JavaMailSender接口实现邮件发送。在Spring Boot的Starter模块中也为此提供了自动化配置。下面通过实例看看如何在Spring Boot中使用JavaMailSender发送邮件。
包含了spring发送邮件的说明文档和代码案例以及图解。
用spring发送电子邮件 在此示例中,我们使用Gmail服务发送电子邮件 应用界面: 如果电子邮件发送成功: 主要依赖 <groupId>org.springframework <artifactId>spring-context-support <version>5.2.5.RELEASE ...
#电子邮件地址 spring.mail.host=smtp.126.com #Application spring.application.name=SEND-MAIL #授权密码 spring.mail.password=password #邮箱服务器默认端口 spring.mail.port=25 #协议 spring.mail.protocol=...
Spring邮件发送系统:基于Java构建,包含67个文件,包括60个XML配置文件、3个Java类文件、2个.gitignore文件、1个Idea项目文件...该项目是一个Spring email的最简单入门案例,用于演示如何使用Spring框架发送电子邮件。
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
Spring的任务执行与调度、Spring Web应用的国际化支持、AJAX和Spring结合的访问模式、利用Spring发 送电子邮件、Spring JMS消息应用编程、教学资源全文检索设计、Java应用的报表打印、网络考试系统设 计、Spring应用...
JEmail发送电子邮件的例子,本Demo对jEmail的API做了包装,开发人员开发应用和学习。支持发送多个附件。另外还采用Struts2+Spring等技术。是一个不错的学习例子
Spring启动与AWS-SES发送电子邮件
第1章 Spring简介 1 1.1 实例化Spring IoC容器 1 1.1.1 问题 1 1.1.2 解决方案 1 1.1.3 工作原理 3 1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用...
使用Thymeleaf模板发送电子邮件并创建它们 使用MockMvcTest,RepositoryTest,EmailTest和用于服务层的常规JUnit4测试进行单元测试的设置 通过Fluentlenium和HtmlUnit与FeatureTest进行快速UI测试 包含SCSS + ...
主要内容包括Spring框架开发环境的搭建及使用Spring时必须掌握的基础知识,Spring的数据持久化、事务管理以及企业应用中的远程调用,JNDI命名服务,JMail发送电子邮件等企业级服务,小型网站或应用程序的开发思路、...
Spring Boot Quartz Scheduler示例:... 该项目正在使用Gmail的SMTP服务器发送电子邮件。 无论您使用Gmail还是其他任何SMTP服务器,都需要相应地配置以下邮件属性- spring.mail.host =smtp.gmail.com spring.mail.p
第1章 Spring简介 1 1.1 实例化Spring IoC容器 1 1.1.1 问题 1 1.1.2 解决方案 1 1.1.3 工作原理 3 1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用...
基于spring的项目管理应用程序。 我的主要目标是为每个人创建一个免费的开源隐私项目管理应用程序。...可选择发送电子邮件通知 仪表板 获取为当前用户分配的任务 用户: 用户名 完成 个性化通知 阻止添加到新项目/团队
该项目依赖于无模板模块it.ozimov:spring-boot-email-core ,该模块提供了核心功能(例如,发送电子邮件,调度和确定优先级,持久性)。 由于它是无模板的,因此它不提供用于通过模板引擎生成电子邮件正文的服务的...
7.2 发送电子邮件 7.3 调度任务 7.3.1 使用Java Timer调度任务 7.3.2 使用Quartz调度器 7.3.3 按调度计划调用方法 7.4 使用JMS发送消息 7.4.1 使用JMS模板发送消息 7.4.2 消费消息 ...
常用的电子邮件协议有SMTP、POP3、IMAP4,它们都隶属于TCP/IP协议簇,默认状态下,分别通过TCP端口25、110和143建立连接。 SMTP协议 SMTP的全称是 “Simple Mail Transfer Protocol”,即简单邮件传输协议。它是一组...
12.2发送电子邮件 12.2.1配置邮件发送器 12.2.2构建电子邮件 12.3调度任务 12.3.1使用javatimer调度任务 12.3.2使用quartz调度器 12.3.3按调度计划调用方法 12.4使用jmx管理springbean 12.4.1将springbean...