springboot调用第三方邮箱发送邮件过程详解

编程入门 行业动态 更新时间:2024-10-27 18:27:42

1 邮箱设置

邮箱首页点击设置 => 账户 => 开启POP3/SMTP服务


没有开启的就开启,如果你已开启但是之前的客户端授权码找不到了就重新开启一遍获取客户端授权码,验证完授权码一定要保存

2 配置文件

spring:
  application:
    name: ccctop-crowd-auth
  thymeleaf:
    check-template-location: true
    prefix: classpath:/templates/
    suffix: .html
    cache: false
  mail:
    host: smtp.qq #发送邮件服务器
    protocol: smtp
    default-encoding: utf-8
    username: 2028965152@qq #发送邮件的邮箱地址
    password:  nvewownyiaxleege #客户端授权码,不是邮箱密码,这个在qq邮箱设置里面自动生成的
    port: 587 #端口号465或587
    from: 2028965152@qq # 发送邮件的地址,和上面username一致
    properties:
      mail:
        stmp:
          ssl:
            enable: true

3 写测试处理器

package com.ccctop.crowd.handler;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

@Slf4j
@Controller
public class MemberHandler {
    @Autowired
    private JavaMailSender javaMailSender;

    @GetMapping("/auth/send/{to}/{subject}/{text}")
    public String sendQQMail(@PathVariable String to,
                                       @PathVariable String subject,
                                       @PathVariable String text){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("2028965152@qq");
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);
        javaMailSender.send(simpleMailMessage);
        return "success";
    }
}

3 测试

使用postman测试如下接口

http://localhost:81/auth/send/2028965152@qq.com/test-subject/test-content 

结果如下(整个过程相当于是我自己给我自己发送邮箱,也就是参数from和to都是我自己的邮箱2028965152@qq)

更多推荐

springboot调用第三方邮箱发送邮件过程详解

本文发布于:2023-06-11 03:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379710.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第三方   发送邮件   详解   邮箱   过程

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!