springboot中使用javamail

来源:互联网 发布:复杂网络 matlab 编辑:程序博客网 时间:2024/05/29 06:56

javaMail的使用

  1. 先来展示下收到邮件的效果图:

  2. 准备工作

    • 邮箱服务器地址:这里使用新浪邮箱,登录你的新浪邮箱,开启SMTP
    • 目标邮箱地址:可以是自己的qq邮箱,能接到邮件就行
  3. maven引入依赖包,thymeleaf有提供htmlmail模板

       <!--mail-->   <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-mail</artifactId>   </dependency>   <!--thymeleaf-->   <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-thymeleaf</artifactId>   </dependency>
  4. 配置相关信息

    spring:   thymeleaf:   cache: false   mode: LEGACYHTML5 mail:     # 邮箱设置里可以找到     host: smtp.sina.com     # 去页面开启SMTP     username:     password:     default-encoding: UTF-8
  5. 编写相关代码

原创粉丝点击