【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 ...
随机推荐
- 非空处理 Java非空判断 非空处理及mysql数据库字段的not null
1.mysql## 去掉非空,如果非空又没有默认值,这样程序在添加数据的时候i,如果没有设置值就会报错.该操作很危险.##ALTER TABLE `order_test` ADD COLUMN `te ...
- email邮件(带附件,模拟文件上传,跨服务器)发送核心代码 Couldn't connect to host, port: smtp.163.com, 25; timeout -1;
邮件(带附件,模拟文件上传,跨服务器)发送核心代码1.测试邮件发送附件接口 /** * 测试邮件发送附件 * @param multipartFile * @return */ @RequestMap ...
- python post or get请求demo
# python post or get请求demo import requests; import json; headers={ "Content-Type":"ap ...
- LiveCharts2:简单灵活交互式且功能强大的.NET图表库
前言 之前的文章中提到过ScottPlot.与oxyplot,这两个是比较常用的.NET图表库,今天介绍一款新的.NET图表库:LiveCharts2. LiveCharts2介绍 LiveChart ...
- openfly:基于nginx的4层代理管理平台
简介 作者:京城郭少 基于nginx的4层代理管理平台 支持的功能: 被动健康检查 白名单 include导入文件 哈希 backup冗余互备 weight权重 注释 ...... 部署openfly ...
- 13-flex
01 flex2个重要的概念 02 flex布局模型 03 flex相关属性 04 flex container相关属性 4.1 flex direction 不同的值会改变主轴的方向 4.2 fle ...
- Android系统源码的整编和单编
# Android系统源码的整编和单编 文章作者: 刘望舒 文章链接: http://liuwangshu.cn/framework/aosp/3-compiling-aosp.html 前言 很多时 ...
- 模拟用户登录-cookes
import requests url = 'https://www.xread8.com/user/login.json' headers = { 'User-Agent': 'Mozilla/5. ...
- 构建高可用性、高性能和可扩展的Zabbix Server架构
简介 本教程讲解了一下如何设计构建一个高性能.高可靠.高扩展的Zabbix 监控集群. 架构图 架构图PDF下载: https://songxwn.com/file/Zabbix_HA.pdf Pig ...
- OpenCV程序:OCR文档扫描
一.文档扫描 代码 import cv2 import numpy as np #==============================计算输入图像的四个顶点的坐标=============== ...