springboot+mail发邮件

springboot发邮件

  • Spring Boot中发送邮件具体的使用步骤如下
    • 添加maven模块依赖
    • 添加Spring Boot配置
      • QQ配置
      • 网易(126/163/yeah)邮箱配置
      • GMAIL邮箱配置
    • 发送普通邮件
    • 喜欢请打赏

Spring Boot中发送邮件具体的使用步骤如下

1、添加maven模块依赖
2、添加Spring Boot配置(QQ/网易系/Gmail)
3、调用JavaMailSender接口发送邮件

添加maven模块依赖

>
    >org.springframework.boot>
    >spring-boot-starter-mail>
>

添加Spring Boot配置

QQ配置

spring:
  mail:
    host: smtp.qq.com #发送邮件服务器
    username: [email protected] #QQ邮箱
    password: xxxxxxxxxxx #客户端授权码
    protocol: smtp #发送邮件协议
    properties.mail.smtp.auth: true
    properties.mail.smtp.port: 465 #端口号465或587
    properties.mail.display.sendmail: Javen #可以任意
    properties.mail.display.sendname: Spring Boot Guide Email #可以任意
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true
    default-encoding: utf-8
    from: [email protected] #与上面的username保持一致

网易(126/163/yeah)邮箱配置

spring:
  mail:
    host: smtp.163.com
    username: [email protected]
    password: xxxxxxxx
    protocol: smtp
    properties.mail.smtp.auth: true
    properties.mail.smtp.port: 994 #465或者994
    properties.mail.display.sendmail: Javen
    properties.mail.display.sendname: Spring Boot Guide Email
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true
    default-encoding: utf-8
    from: [email protected]

GMAIL邮箱配置

spring:
  mail:
    host: smtp.gmail.com
    username:[email protected]
    password: xxxxx #Gmail账号密码
    protocol: smtp
    properties.mail.smtp.auth: true
    properties.mail.smtp.port: 465
    properties.mail.display.sendmail: Javen
    properties.mail.display.sendname: Spring Boot Guide Email
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true
    from: [email protected]
    default-encoding: utf-8

发送普通邮件

 public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

喜欢请打赏

springboot+mail发邮件_第1张图片

你可能感兴趣的:(springboot)