需要的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. Maven 指令 mvn:dependency:tree 查看依赖

    一.指令指导文档: 官方文档:https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html https://maven ...

  2. Vue学习:15.组件化开发

    组件化开发 组件化开发是一种软件开发方法,它将应用程序拆分成独立的.可重用的模块,每个模块都被称为组件.这些组件可以独立开发.测试.维护和部署,从而提高了代码的可维护性.可扩展性和复用性.在前端开发中 ...

  3. llm-universe - 1

    Smiling & Weeping ---- 难怪春迟迟不来,原来是我把雪一读再读 一.大型语言模型(LLM)理论简介 1 大型语言模型(LLM)的概念 大语言模型(LLM,Large Lan ...

  4. svn服务端安装和使用

    首先去官网下载安装包 点我下载 下载完了以后选择安装路径然后一直next就可以了 安装完了以后在开始菜单里面找到svn 打开  如何使用? 这里是创建代码管理的存储库 点击 repositories ...

  5. 你了解Vim的增删改查吗 ?

    增: 在Vim的Normal模式中输入A/I/O,a/i/o字符进行对应的增加操作. 删 在Vim的Normal模式中, 输入x 删除光标对应的一个字符(4x代表删除4个字符): 输入dd删除光标所在 ...

  6. .Net Framework使用Autofac实现依赖注入

    .Net Framework使用Autofac实现依赖注入 前言 最近也是找了快2周的工作了,收到的面试邀请也就几个,然后有个面试题目是用asp.net mvc + Entityframework 做 ...

  7. 高通Android平台 电池 相关配置

    背景 在新基线上移植有关的代码时,在log中发现有关的东西,请教了有关的同事以后,解决了这个问题. [ 12.775863] pmi632_charger: smblib_eval_chg_termi ...

  8. hynitron ts 驱动分析

    # hynitron ts 驱动分析 背景 在公司项目中搞LCD移植的时候,在TP功能上,有时候频繁操作屏幕时会导致i2c总线返回-2错误. 问题描述: 1.安卓桌面起来以后,点击屏幕有响应. 2.此 ...

  9. python后端model模板

    from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation from django.contri ...

  10. CvT:微软提出结合CNN的ViT架构 | 2021 arxiv

    CvT将Transformer与CNN在图像识别任务中的优势相结合,从CNN中借鉴了多阶段的层级结构设计,同时引入了Convolutional Token Embedding和Convolutiona ...