Spring的javaMailSender对JavaMail进行封装,简化了开发,告别繁琐的API。
1. SpringMail开发步骤如下
1.1 引入依赖
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
1.2 spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<!-- 发送邮件的协议 -->
<property name="protocol" value="smtp"/>
<!-- 邮件服务器地址 -->
<property name="host" value="smtp.qq.com"/>
<!-- 发送邮件的端口 -->
<property name="port" value="465" />
<!-- 账号 -->
<property name="username" value="发件人邮箱"/>
<!-- 账号的密码 -->
<property name="password" value="发件人邮箱的密码(可能是第三方的秘钥)"/>
<!-- mail的配置 -->
<property name="javaMailProperties">
<props>
<!-- 验证:用户名username 与发件是否一致 开启认证,会校验发件人是否为邮箱用户 -->
<prop key="mail.smtp.auth">true</prop>
<!-- 传输层的加密 采用SSL安全方式-->
<prop key="mail.smtp.ssl.enable">true</prop>
</props>
</property>
</bean>
<context:component-scan base-package="cn.bjc.demo"></context:component-scan>
</beans>
1.3 测试类
package cn.bjc.demo;
import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
@Component
public class TestSend {
@Resource
private JavaMailSender javaMailSender;
public void sendMail() throws Exception{
//1. 创建邮件信息
MimeMessage message = javaMailSender.createMimeMessage();
//2. 使用spring邮件工具类
MimeMessageHelper helper = new MimeMessageHelper(message);
//3.收件人
helper.setTo("收件人邮箱");
//4.发件人
helper.setFrom("发件人邮箱");
//5.设置邮件的标题
helper.setSubject("库存预警");
//6.邮件的内容
helper.setText("能收到么?");
//7.发送邮件
javaMailSender.send(message);
}
}
到这里SpringMail的小功能就完成了,比用原生的API简单方便的多。
2. 使用SpringTask实现定时发送邮件的功能
2.1 依赖引入
只要引入mail的依赖于spring相关的依赖即可
<!-- 添加mail依赖 -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
2.2 编写发动邮件的工具类
package cn.bjc.utils;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
/**
* 邮件工具类
* @author Administrator
*
*/
public class MailUtil {
private JavaMailSender javaMailSender;
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
//发件人
private String from;
public void sendMail(String to, String subject, String text) throws MessagingException{
//1. 创建邮件信息
MimeMessage message = javaMailSender.createMimeMessage();
//2. 使用spring邮件工具类
MimeMessageHelper helper = new MimeMessageHelper(message);
//3.收件人
helper.setTo(to);
//4.发件人
helper.setFrom(from);
//5.设置邮件的标题
helper.setSubject(subject);
//6.邮件的内容
helper.setText(text);
//7.发送邮件
javaMailSender.send(message);
}
public void setFrom(String from) {
this.from = from;
}
}
2.3 发送邮件的spring配置文件
applicationContext_mail.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<!-- 发送邮件的协议 -->
<property name="protocol" value="smtp"/>
<!-- 邮件服务器地址 -->
<property name="host" value="smtp.qq.com"/>
<!-- 发送邮件的端口 -->
<property name="port" value="465" />
<!-- 账号 -->
<property name="username" value="发件人邮箱"/>
<!-- 账号的密码 -->
<property name="password" value="发件人密码"/>
<!-- mail的配置 -->
<property name="javaMailProperties">
<props>
<!-- 验证:用户名username 与发件是否一致 开启认证,会校验发件人是否为邮箱用户 -->
<prop key="mail.smtp.auth">true</prop>
<!-- 传输层的加密 采用SSL安全方式-->
<prop key="mail.smtp.ssl.enable">true</prop>
</props>
</property>
</bean>
<bean id="mailUtil" class="cn.bjc.utils.MailUtil">
<property name="javaMailSender" ref="javaMailSender"></property>
<!-- 发件人 -->
<property name="from" value="发件人邮箱"></property>
</bean>
</beans>
2.4 任务Jon类
package cn.bjc.biz.timer;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import cn.bjc.biz.IStorealertBiz;
import cn.bjc.entity.Storealert;
@Component
public class StoreMailJob {
@Resource
private IStorealertBiz storealertBiz;
/*
*
* 每天上午12:20分和下午16:20执行
* 0 20 12,16 * * ?
*
* */
@Scheduled(cron="0 20 12,16 * * ?")
public void sendStorealertMail(){
System.out.println("执行了任务调度。。。");
//查询是否存在库存预警
List<Storealert> storealertList = storealertBiz.getAll(null);
if(storealertList.size() > 0){
try {
//调用 业务发送预警邮件
storealertBiz.sendStorealertMail();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
2.5 任务调度配置
applicationContext_job.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
<!--配置包扫描-->
<context:component-scan base-package="cn.bjc.biz.timer" />
<task:executor id="executor" pool-size="5"/>
<task:scheduler id="scheduler" pool-size="10"/>
<!-- 定时任务配置 scheduler 方式 注解 这句就是标明注解方式 -->
<task:annotation-driven executor="executor" scheduler="scheduler"/>
</beans>