需要的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】的更多相关文章

  1. 【Spring实战】----开篇(包含系列目录链接)

    [Spring实战]----开篇(包含系列目录链接) 置顶2016年11月10日 11:12:56 阅读数:3617 终于还是要对Spring进行解剖,接下来Spring实战篇系列会以应用了Sprin ...

  2. 【Spring开发】—— Spring Core

    原文:[Spring开发]-- Spring Core 前言 最近由于一些工作的需要,还有自己知识的匮乏再次翻开spring.正好整理了一下相关的知识,弥补了之前对spring的一些错误认知.这一次学 ...

  3. 【Spring实战】Spring注解配置工作原理源码解析

    一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...

  4. 【Spring实战】Spring容器初始化完成后执行初始化数据方法

    一.背景知识及需求 在做WEB项目时,经常在项目第一次启动时利用WEB容器的监听.Servlet加载初始化等切入点为数据库准备数据,这些初始化数据是系统开始运行前必须的数据,例如权限组.系统选项.默认 ...

  5. 【转】【Spring实战】Spring注解配置工作原理源码解析

    一.背景知识 在[Spring实战]Spring容器初始化完成后执行初始化数据方法一文中说要分析其实现原理,于是就从源码中寻找答案,看源码容易跑偏,因此应当有个主线,或者带着问题.目标去看,这样才能最 ...

  6. 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用

    2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...

  7. 【Spring Session】和 Redis 结合实现 Session 共享

    [Spring Session]和 Redis 结合实现 Session 共享 参考官方文档 HttpSession with Redis Guide https://docs.spring.io/s ...

  8. 【Spring Boot】定时任务

    [Spring Boot]定时任务 测试用业务Service package com.example.schedule.service; import org.springframework.ster ...

  9. 【spring boot】配置信息

    ======================================================================== 1.feign 超时配置 2.上传文件大小控制 3.J ...

  10. 【Spring MVC】Properties文件的加载

    [Spring MVC]Properties文件的加载 转载:https://www.cnblogs.com/yangchongxing/p/10726885.html 参考:https://java ...

随机推荐

  1. Grafana 开源了一款 eBPF 采集器 Beyla

    eBPF 的发展如火如荼,在可观测性领域大放异彩,Grafana 近期也发布了一款 eBPF 采集器,可以采集服务的 RED 指标,本文做一个尝鲜介绍,让读者有个大概了解. eBPF 基础介绍可以参考 ...

  2. java怎样把两个list里边相同的数据取出

    1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class I { 5 6 public static void m ...

  3. Apache Shiro 的三大核心组件

    a.Subject :当前用户的操作 b.SecurityManager:用于管理所有的Subject c.Realms:用于进行权限信息的验证

  4. flutter 尝试创建第一个页面(三)

    新建目录 assets  存放图片 在pubspec..yaml 中添加 flutter: # The following line ensures that the Material Icons f ...

  5. Externalizable接口实现序列化与反序列化

    Externalizable接口实现序列化与反序列化 package com.example.core.mydemo.java; import com.example.core.mydemo.json ...

  6. scab2

    package com.cmb.cox.utils;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;im ...

  7. 【vue】利用输入框搜索过滤来选择列表

    方法1 <div id="app"> <input type="text" @input="handleInput()" ...

  8. Ethercat设备数据 转 EthernetIP项目案例

    1         案例说明 1.   设置网关采集EtherCAT设备数据 2.   把采集的数据转成EthernetIP协议转发给其他系统. 2        VFBOX网关工作原理 VFBOX网 ...

  9. Xilinx-HDF的文件内容

    Xilinx-HDF文件 原文:分享:HDF文件的更多用途 Xilnx Vivado能导出HDF文件,给Xilnx SDK创建软件工程.HDF文件的还可以有更多用途. HDF文件是一个zip文件,可以 ...

  10. LLM并行训练4-megascale论文学习

    算法优化 并行注意力机制 \[串行版本: y = x + MLP(LayerNorm(x + Attention(LayerNorm(x)))) \] \[并行版本: y = x + MLP(Laye ...