springboot发送邮箱

spring Boot中发送邮件使用的是Spring提供的org.springframework.mail.javamail.JavaMailSender,其提供了许多简单易用的方法,可发送简单的邮件、HTML格式的邮件、带附件的邮件,并且可以创建邮件模板。

项目地址:https://github.com/heng1234/springboot2.x/tree/master/boot_email

pom引入相关jar

<!--邮件发送jar-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>2.1.7.RELEASE</version>
</dependency>
<!--thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.1.7.RELEASE</version>
</dependency>

application.yml

server:
  port: 7010
spring:
  mail:
    #email
    #SMTP服务器的名字
    host: smtp.mxhichina.com
    #发送人邮箱名称
    username: xxxx@qq.com
    #邮箱密码,这里指的是,在邮箱中打开SMTP/POP3验证之后,会给出一个验证码
    password: 123456
    #编码格式
    default-encoding: UTF-8
    #SMTP服务器开放的端口
    port: 465
    properties:
      mail:
        smtp:
          socketFactory:
            class: javax.net.ssl.SSLSocketFactory

MailUtil

测试MailController

emailTemplate.html

项目结构

image-20191111154535922

自定义模板访问:http://localhost:7010/email/tempMail?email=1350047452@qq.com

image-20191111154627119

发送图片访问:http://localhost:7010/email/imgMail?email=1350047452@qq.com

image-20191111154737041

带附件访问:http://localhost:7010/email/attachmentMail?email=1350047452@qq.com

image-20191111154829117

下一章:springboot使用swagger2

Last updated

Was this helpful?