1.需要mail-1.4.1.jar包,配置一个属性文件email.properties

#邮箱服务器
email.host=smtp.163.com
#邮箱账号
email.username=z19160343743@163.com
#邮箱SMTP的授权码,不是邮箱密码
email.password=xxxxxxxxxxxx

2.编写applicationContext-email.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd"><!--该标签的作用是向spring容器中注册4个BeanPostProcessor,这样就可以直接使用@Autowired、@Resource 、@PostConstruct、@PreDestroy--><context:annotation-config/><!-- 引入属性文件 --><context:property-placeholder location="classpath:email.properties" /><bean  id ="mailSender"  class ="org.springframework.mail.javamail.JavaMailSenderImpl" ><!--服务器--><property name="host" value="${email.host}" /><!--邮箱账号--><property name="username" value="${email.username}" /><!--授权码--><property name="password" value="${email.password}" /><!--设置默认编码格式--><property name="defaultEncoding" value="UTF-8"></property><!--SMTP服务器验证--><property name="javaMailProperties"><props><!--开启权限验证--><prop key="mail.smtp.auth">true</prop><!--设置链接超时时间--><prop key="mail.smtp.timeout">25000</prop><!--设置服务器端口号--><prop key="mail.smtp.port">25</prop></props></property></bean ><bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage"><!--发件人的邮箱账号--><property name="from" value="${email.username}" /></bean><!--将属性注入到工具类中--><bean id="mailUtil" class="com.zengjx.email.utils.EmailUtil"><property name="mailSender" ref="mailSender"></property><property name="simpleMailMessage" ref="simpleMailMessage"></property></bean></beans>

3.在主配置文件applicationContext.xml中导入applicationContext-email.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--导入applicationContext-email.xml文件--><import resource="applicationContext-email.xml"/><!--设置扫描--><context:component-scan base-package="com.zengjx.email" /></beans>

4.在web.xml中开启对applicationContext.xml文件的监听

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><listener><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener><!--监听applicationContext.xml文件--><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></init-param><load-on-startup>0</load-on-startup></servlet><!--设置拦截--><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--设置首页--><welcome-file-list><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list></web-app>

5.编写一个工具类,用来发送邮件

package com.zengjx.email.utils;import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;/*** @author: FastRabbit* @date: 2021/4/11 17:08* @description:*/
public class EmailUtil {//邮箱发送者private MailSender mailSender;//邮件private SimpleMailMessage simpleMailMessage;//将xml配置MailSender的bean注入到该字段中public void setMailSender(MailSender mailSender) {this.mailSender = mailSender;}//将xml配置SimpleMailMessage的bean注入到该字段中public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {this.simpleMailMessage = simpleMailMessage;}/*** @param recipient 收件人* @param subject 主题* @param content 内容*/public void send(String recipient,String subject,String content){simpleMailMessage.setTo(recipient);simpleMailMessage.setSubject(subject);simpleMailMessage.setText(content);mailSender.send(simpleMailMessage);}}

6.测试一下

package com.zengjx.email.test;import com.zengjx.email.utils.EmailUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** @author: FastRabbit* @date: 2021/4/11 17:55* @description:*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSend {@AutowiredEmailUtil emailUtil;@Testpublic void testSendEmail(){emailUtil.send("1095962937@qq.com","洞拐洞拐,收到请回答","请求活力支援");}}

spring框架使用JavaMailSenderImpl发送邮件相关推荐

  1. java发邮件的框架_Java的Spring框架中实现发送邮件功能的核心代码示例

    Spring中已经封装了邮件操作类,通过spring配置文件可以便捷地注入到controller.action等地方. 下面是配置: p:host="${mail.host}" p ...

  2. java邮件模板代码_Java的Spring框架中实现发送邮件功能的核心代码示例

    Spring中已经封装了邮件操作类,通过spring配置文件可以便捷地注入到controller.action等地方. 下面是配置: p:host="${mail.host}" p ...

  3. spring 框架-java配置163邮件服务器,发送邮件

    java配置163邮件服务器,发送邮件 首先保证spring框架包的正确导入 1.利用spring的邮件服务 进行配置邮件服务器 再导入:javax.mail.jar这是邮件服务的插件实现 sprin ...

  4. 利用Spring框架封装的JavaMail现实同步或异步邮件发送

    利用Spring框架封装的JavaMail现实同步或异步邮件发送 作者:张纪豪 J2EE简单地讲是在JDK上扩展了各类应用的标准规范,邮件处理便是其中一个重要的应用.它既然是规范,那么我们就可以通过J ...

  5. spring框架aop_使用Spring框架和AOP进行动态路由

    spring框架aop 本文的总体思路是展示业务交易如何动态触发业务事件以进行子系统处理. 本文显示的示例有效地使用了Spring框架2.0和Spring AOP来将业务服务与子系统处理功能分离. 现 ...

  6. java 路由框架_使用Spring框架和AOP实现动态路由

    本文的大体思路是展示了一次业务交易如何动态地为子系统处理过程触发业务事件.本文所示的例子使用Spring框架和Spring AOP有效地解耦业务服务和子系统处理功能.现在让我们仔细看看业务需求. 业务 ...

  7. 利用Spring框架封装的JavaMail实现同步或异步邮件发送

    J2EE简单地讲是在JDK上扩展了各类应用的标准规范,邮件处理便是其中一个重要的应用.它既然是规范,那么我们就可以通过JDK遵照邮件协议编写一个邮件处理系统,但事实上已经有很多厂商和开源组织这样做了. ...

  8. Spring Boot——基于spring-boot-starter-mail发送邮件的 Service 服务类DEMO

    前言 发送邮件应该是网站的必备拓展功能之一,注册验证,忘记密码或者是给用户发送营销信息.正常我们会用JavaMail相关api来写发送邮件的相关代码,但现在Spring Boot提供了一套集成spri ...

  9. spring 框架发送 simpleMail email

    在网上找了spring框架下发送email的例子,结果发现发生 533错误 553 authentication is required 找了半天也没有发现实质解决的办法,偶然发现居然没有对messa ...

最新文章

  1. ElasticSearch + xpack 使用
  2. 大脑“拖延症”让你直呼“眼瞎”:加工视觉信息有15秒延迟 | Science子刊
  3. Java多线程学习六:使用线程池比手动创建线程好在那里以及常用线程池参数的意义
  4. Microsoft Research和Windows Azure合作伙伴对数据发现和共享的影响
  5. atitit.atiLinq v2新特性attilax大总结 q326
  6. 数字图像直方图匹配或规定化Histogram Matching (Specification)处理
  7. 基于SSM毕业生就业管理系统
  8. 亚信安全助手、杀毒软件卸载
  9. 压缩文件密码解密工具介绍
  10. 计算机碎片整理的作用,经常做磁盘碎片整理的好处
  11. 【Linux】安装网易云全攻略
  12. tp5 给图片加水印
  13. Protobuf简单使用
  14. PC端、手机端在线预览文档组件react-file-viewer与npm构建内存溢出
  15. py233基于 python的诚交大学生二手交易平台Django#毕业设计
  16. BZOJ3837 : [Pa2013]Filary
  17. 批量识别图片文字并存为Excel,几行Python轻松实现!
  18. 基音检测算法的性能:Performance Evaluation of Pitch Detection Algorithms
  19. 论文中公式后标点的使用
  20. 区块链的共识机制是什么?

热门文章

  1. 限制网页只能在微信打开
  2. GPS软件接收机(3)——跟踪
  3. 投稿动态无法删除的情况下,如何批量删除B站动态?
  4. 微信开发者工具 缓存目录
  5. 能力清单:透视成功逻辑,学会清单练习
  6. h5 富文本输入框_H5富文本编辑器的详细介绍
  7. SQL SERVER 多字段不为空COALESCE用法
  8. 儿童成长曲线 (WHO标准)
  9. jzoj2555 雾雨魔理沙
  10. layui 加载loding图标