SpringJavaMailSender Spring自带的邮件推送功能实现
Posted 丶会飞的羊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringJavaMailSender Spring自带的邮件推送功能实现相关的知识,希望对你有一定的参考价值。
备注:JavaMailSender在spring-context-support.jar中
1.配置spring-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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- spring mail begin ############################################## --> <bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>/mail.properties</value> </list> </property> </bean> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <bean id="MailSenderDemo" class="com.zlg.test.springmail.MailSenderDemo"></bean> <!-- spring mail demo end ############################################## --> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${mail.host}"></property> <property name="port" value="${mail.port}"></property> <property name="username" value="${mail.username}"></property> <property name="password" value="${mail.password}"></property> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.timeout">25000</prop> <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> <!-- 如果是网易邮箱, mail.smtp.starttls.enable 设置为 false--> <prop key="mail.smtp.starttls.enable">true</prop> </props> </property> </bean> </beans>
mail.smtp.socketFactory.class 加上该配置,避免出现认证错误
mail.properties
# qq mail server
#mail.protocol=smtp
#mail.port=465
#mail.host=smtp.exmail.qq.com
#mail.username=xxx@qq.com
#mail.password=
# 163 mail server
mail.protocol=smtp
mail.port=465
mail.host=smtp.163.com
mail.username=xxx@163.com
mail.password=
2.web.xml中加入bean配置
<servlet> <servlet-name>provider</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> <!-- classpath:applicationContext.xml, --> <!-- classpath:applicationContext-servlet.xml, --> <!-- classpath:dubbo.xml, --> classpath:spring-mail.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>provider</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
3.pom.xml中加入jar包依赖
<!-- spring mail需要的jar包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
4.编写Java接口文件
package com.zlg.test.springmail; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; /** * @Title: MailSenderDemo.java * @Package: com.zlg.test.springmail * @Description: spring mail 测试 * @author: zlg * @date: 2017年6月20日 上午9:43:45 * @version: V1.0 */ public class MailSenderDemo { @Autowired private JavaMailSender mailSender; public void send(SimpleMailMessage mail) { mailSender.send(mail); } public JavaMailSender getMailSender(){ return this.mailSender; } }
5.添加测试类
package com.zlg.test.springmail; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; /** * @Title: SendTest.java * @Package: com.zlg.test.springmail * @Description: TODO * @author: zlg * @date: 2017年6月20日 上午10:03:36 * @version: V1.0 */ public class SendTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext( "spring-mail.xml"); MailSenderDemo sender = (MailSenderDemo) ac.getBean("MailSenderDemo"); /*SimpleMailMessage mail = new SimpleMailMessage(); mail.setTo("673376601@qq.com");// 收件人邮箱地址 mail.setFrom("zlg");// 收件人 mail.setSubject("SpringMailTest");// 主题 mail.setText("测试Spring自带邮件发送功能");// 正文 sender.send(mail);*/ JavaMailSender javaMailSender = sender.getMailSender(); MimeMessage mime = javaMailSender.createMimeMessage(); MimeMessageHelper helper; try { helper = new MimeMessageHelper(mime, true, "utf-8"); helper.setTo("dszlg@163.com");// 收件人邮箱地址 helper.setFrom("dszlg@163.com");// 收件人 helper.setSubject("SpringMailTest");// 主题 helper.setText("测试Spring自带邮件发送功能");// 正文 } catch (MessagingException me) { me.printStackTrace(); } javaMailSender.send(mime); } }
6.查看效果
以上是关于SpringJavaMailSender Spring自带的邮件推送功能实现的主要内容,如果未能解决你的问题,请参考以下文章
java ssm 后台框架平台 项目源码 websocket 即时通讯 IM quartz spri
Spring Boot 揭秘与实战 服务器篇 - 其他内嵌服务器 发表于 2017-01-03 | Spring框架 | Spri
Java springcloud B2B2C o2o多用户商城 spri:熔断器Hystrix
Non-resolvable parent POM for com.example:dubbo:0.0.1-SNAPSHOT: Could not transfer artifact org.spri