【Spring-Mail】
需要的pom依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.9.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.9.RELEASE</version>
</dependency> <dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
一、普通邮件发送
编写配置信息【app.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="cn.zeal4j"/> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.qq.com" />
<property name="port" value="587" />
<property name="username" value="zeal4j" />
<property name="password" value="frcgbdjqjzptbegg" />
<property name="defaultEncoding" value="utf-8"/>
</bean> <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="zeal4j@qq.com" />
<property name="subject" value="spring-mail"/>
</bean>
</beans>
管理器类:
package cn.zeal4j.util.springmail; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailMessage;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component; /**
* @author Administrator
* @file IntelliJ IDEA Spring-Mail
* @create 2020 09 22 12:38
*/
@Component
public class MailOrderManager implements OrderManager{ @Autowired
private MailSender mailSender;
@Autowired
private MailMessage mailMessage; public void placeOrder() {
mailMessage.setTo("zeal4j@qq.com");
mailMessage.setText("This message powered by Spring-Mail, 测试");
mailSender.send((SimpleMailMessage) mailMessage);
}
}
接口:
package cn.zeal4j.util.springmail; /**
* @author Administrator
* @file IntelliJ IDEA Spring-Mail
* @create 2020 09 22 12:39
*/
public interface OrderManager { void placeOrder();
}
测试:
@Test
public void sendSpringMail() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app.xml");
MailOrderManager mailOrderManager = applicationContext.getBean("mailOrderManager", MailOrderManager.class);
mailOrderManager.placeOrder();
}
二、带附件邮件发送:
@Test
public void sendSpringMailWithAttachment() throws Exception{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app.xml"); JavaMailSender mailSender = applicationContext.getBean("mailSender", JavaMailSender.class); MimeMessage mimeMessage = mailSender.createMimeMessage(); mimeMessage.setSubject("Spring-Mail 附件测试"); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8"); mimeMessageHelper.setTo("zeal4j@qq.com"); mimeMessageHelper.setFrom("zeal4j@qq.com"); mimeMessageHelper.setText("Spring-Mail 附件测试 ......"); File file = new File("D:\\picture\\collection\\bg.jpg"); mimeMessageHelper.addAttachment(file.getName(), file); mailSender.send(mimeMessage);
}
【Spring-Mail】的更多相关文章
- 【Spring实战】----开篇(包含系列目录链接)
[Spring实战]----开篇(包含系列目录链接) 置顶2016年11月10日 11:12:56 阅读数:3617 终于还是要对Spring进行解剖,接下来Spring实战篇系列会以应用了Sprin ...
- 【Spring开发】—— Spring Core
原文:[Spring开发]-- Spring Core 前言 最近由于一些工作的需要,还有自己知识的匮乏再次翻开spring.正好整理了一下相关的知识,弥补了之前对spring的一些错误认知.这一次学 ...
- 【Spring实战】Spring注解配置工作原理源码解析
一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...
- 【Spring实战】Spring容器初始化完成后执行初始化数据方法
一.背景知识及需求 在做WEB项目时,经常在项目第一次启动时利用WEB容器的监听.Servlet加载初始化等切入点为数据库准备数据,这些初始化数据是系统开始运行前必须的数据,例如权限组.系统选项.默认 ...
- 【转】【Spring实战】Spring注解配置工作原理源码解析
一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...
- 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用
2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...
- 【Spring Session】和 Redis 结合实现 Session 共享
[Spring Session]和 Redis 结合实现 Session 共享 参考官方文档 HttpSession with Redis Guide https://docs.spring.io/s ...
- 【Spring Boot】定时任务
[Spring Boot]定时任务 测试用业务Service package com.example.schedule.service; import org.springframework.ster ...
- 【spring boot】配置信息
======================================================================== 1.feign 超时配置 2.上传文件大小控制 3.J ...
- 【Spring MVC】Properties文件的加载
[Spring MVC]Properties文件的加载 转载:https://www.cnblogs.com/yangchongxing/p/10726885.html 参考:https://java ...
随机推荐
- JavaScript语法形式2 内部式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 解决TypeError: 'NoneType' object is not subscriptable
1.捕获异常的方式try: img_list = img_list["name"]except: img_list = "" 2.对象进行判断if img_li ...
- ps top命令查看内存空间
[root@VM-4-3-centos local]# ps aux --sort -rss | head USER PID %CPU %MEM VSZ RSS TTY STAT START TIME ...
- hbase的优缺点
一. 一个关于hbase介绍全面的博客地址 https://www.csdn.net/gather_22/MtTaEgysNjYwOS1ibG9n.html 优点: 1,方便高效的压缩数据. 2,支持 ...
- Springboot3.0+spring6.0+JDK17+配置jsp和打war包
由于某些缘故,公司的产品需要升级,但并不希望花费大量时间重写前端代码(原来的就不是前后分离的).所以虽然spring和springboot都升级为最新的版本,但是依然还是需要支持jsp,并继续用打包为 ...
- 在KEIL中用JTAG仿真出错:error:cannot load driver".....JL2CM3.dll" 时的解决方法
在KEIL中用JTAG仿真出错:error:cannot load driver".....JL2CM3.dll" 时的解决方法 报错: Error:Cannot load dri ...
- 复习 - ajax
复习呢有一个很直观的感受,就是以前学的东西,萌懂半懂的,这一来全部都清楚了,你以前以为你学的并不好但是复习一次把以前的案例一做,居然能够自己独立完成,知识点看着掌握的还不错. 1. 两天时间就把整个a ...
- ARC108C
考虑一颗树怎么染色. 每个子节点染成边的颜色,如果与父亲节点相同,就随便染色(这条边的限制已经被父亲节点满足). 那么一定可以染色. 所以把原图跑最小生成树再按上述方法染色即可. 倘若原图不连通,那么 ...
- 典型性相关分析在SPSS中的实现
典型性相关分析是研究两组变量(每组变量中都可能有多个指标)之间相关关系的一种多元统计方法.它能够揭示出两组变量之间的内在联系. 本文着重模型在spss中的应用,通过一道例题解释各个指标的意义.详细推导 ...
- ROS2开发BUG记录:在将 use_sim_timer 置为 true 时,节点的 Timer_Callback 行为“异常”
问题: 在将 use_sim_timer 置为 true 时,节点 Timer_Callback 行为 "异常" .在回调函数中,使用 self.get_logger().info ...