栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

利用SpringBoot整合邮箱(QQ邮箱)发送验证码邮件

Java 更新时间:发布时间: 百科书网 趣学号

文章目录
  • 前言
  • 一、配置application.properties
  • 二、核心代码
    • 1.Controller层
    • 2.service层
    • 3.工具类
  • Swagger测试


前言

添加pom.xml依赖

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

一、配置application.properties
#邮箱密码验证
spring.mail.default-encoding=UTF-8
spring.mail.username=xxxxxxxxx@qq.com // qq邮箱
spring.mail.password=aaaxxxxxxbffc // 授权码
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true
二、核心代码 1.Controller层

代码如下:

 	@ApiOperation("qq邮箱验证方式")
    @GetMapping("/sendEmail/{email}")
    public Result sendEmail(@PathVariable String email){
        String code = RandomUtil.getSixBitRandom();
        boolean isSuccess = msmService.sendEmail(email,code);
        if (isSuccess) {
            return Result.ok();
        }else {
            return Result.error();
        }
    }
2.service层

代码如下:

 @Override
    public boolean sendEmail(String email, String code) {
        try {
            MailUtils.sendMail(email, Integer.parseInt(code));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
3.工具类
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class EmailMsg implements InitializingBean {
    @Value("${spring.mail.username}")
    private String username;

    @Value("${spring.mail.password}")
    private String password;

    @Value("${spring.mail.host}")
    private String host;

    public static String EMAIL_USERNAME;
    public static String EMAIL_PASSWORD;
    public static String EMAIL_HOST;

    @Override
    public void afterPropertiesSet() throws Exception {
        EMAIL_HOST = host;
        EMAIL_USERNAME =username;
        EMAIL_PASSWORD =password;
    }
}

public class MailUtils {
    public static void sendMail(String to, int vcode) throws Exception {
        //设置发送邮件的主机
        String host = EmailMsg.EMAIL_HOST;
        //1.创建连接对象,连接到邮箱服务器
        Properties props = System.getProperties();
        //Properties 用来设置服务器地址,主机名
        //设置邮件服务器
        props.setProperty("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        //SSL加密
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);
        //props:用来设置服务器地址,主机名;Authenticator:认证信息
        Session session = Session.getDefaultInstance(props, new Authenticator() {
            //通过密码认证信息
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //new PasswordAuthentication(用户名, password);
                //这个用户名密码就可以登录到邮箱服务器了,用它给别人发送邮件
                return new PasswordAuthentication(EmailMsg.EMAIL_USERNAME, EmailMsg.EMAIL_PASSWORD);
            }
        });
        try {
            Message message = new MimeMessage(session);
            //2.1设置发件人:
            message.setFrom(new InternetAddress(EmailMsg.EMAIL_USERNAME));
            //2.2设置收件人 这个TO就是收件人
            message.setRecipient(RecipientType.TO, new InternetAddress(to));
            //2.3邮件的主题
            message.setSubject("来自在线教学平台网站验证码邮件");
            //2.4设置邮件的正文 第一个参数是邮件的正文内容 第二个参数是:是文本还是html的连接
            message.setContent("【在线教学平台网站】验证码邮件,请接收你的验证码:你的验证码是:" + vcode + ",请妥善保管好你的验证码!", "text/html;charset=UTF-8");
            //3.发送一封激活邮件
            Transport.send(message);
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}

Swagger测试


邮件就收到了 ===>

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/601775.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号