• 邮件发送需要引入spring-boot-starter-mail
  • Spring Boot 自动配置MailSenderAutoConfiguration
  • 定义MailProperties内容,配置在application.yml中
  • 自动装配JavaMailSender
  • 测试邮件发送

  1. pom文件配置:

        <!--邮件发送-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
  2. applicationproperties配置:
    spring.mail.username=442624769@qq.com
    #自己邮箱的授权码
    spring.mail.password=lufufllqrylobijg
    spring.mail.host=smtp.qq.com #开启安全(有时需要)
    spring.mail.properties.mail.smtp.ssl.enable=true
  3. 测试类:
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class Springboot04TaskApplicationTests { @Autowired
    JavaMailSenderImpl mailSender; @Test
    public void contextLoads() {
    //创建一个简单的消息邮件
    SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
    simpleMailMessage.setSubject("通知-今晚开会");
    simpleMailMessage.setText("今晚7点30开会"); simpleMailMessage.setTo("2163370170@qq.com");
    simpleMailMessage.setFrom("442624769@qq.com");
    mailSender.send(simpleMailMessage);
    } @Test
    public void test02() throws MessagingException {
    //创建一个复杂的消息邮件 MimeMessage mimeMessage = mailSender.createMimeMessage();
    //准备上传文件
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
    //邮件设置
    helper.setSubject("通知-今晚开会");
    //设置写的这段内容为html
    helper.setText("<b style='color:red'>今天7:30开会</b>",true); helper.setTo("2163370170@qq.com");
    helper.setFrom("442624769@qq.com"); //上传文件
    helper.addAttachment("1.png",new File("C:\\Users\\44262\\Desktop\\1.png")); mailSender.send(mimeMessage); }
    }

最新文章

  1. MyEclipse新建web project和navicat110_mysql_en工具
  2. autobench 测试笔记
  3. Android 注解工具 ButterKnife
  4. java数据结构
  5. 怎么通过activity里面的一个按钮跳转到另一个fragment(android FragmentTransaction.replace的用法介绍)
  6. python中列表和元组的使用方法和区别
  7. 安装python2.7.13-64bit &amp; Pycharm在两个python版本之间切换
  8. PHP初体验
  9. 2017&quot;百度之星&quot;程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】
  10. 微软Azure AspNetCore微服务实战第2期
  11. linux C中调用shell命令和运行shell脚本
  12. VUE v-for问题
  13. 第一模块 Python开发入门
  14. An optimizer that trains as fast as Adam and as good as SGD. https://www.luolc.com/publications/ad…
  15. Flink安装启动
  16. 如何恢复windows的exe文件的默认打开方式
  17. Java读取maven目录下的*.properties配置文件
  18. vuejs code splitting with webpack 3种模式
  19. Android笔记-1
  20. Linux对包管理阐述

热门文章

  1. React:styled-components
  2. Selenium(一)---Selenium的安装和使用
  3. 如何去掉vue路由中的#
  4. HTML_CSS使用
  5. iOS开发系列-NSFileManager
  6. GetOpenFilename的基本用法
  7. 2019牛客暑期多校训练营(第八场) E 线段树+可撤销并查集
  8. Android studio 添加引用Module项目 与 设置Module项目的Libs的Jar在主项目里使用
  9. CSS——精灵技术
  10. 最大流Dinic算法的一些优化 [网络流][最大流]