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


前言

添加pom.xml依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
 </dependency>

一、配置application.properties

二、核心代码

1.Controller层

代码如下:

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("<h2>【在线教学平台网站】验证码邮件,请接收你的验证码:</h2><h3>你的验证码是:" + vcode + ",请妥善保管好你的验证码!</h3>", "text/html;charset=UTF-8");
            //3.发送一封激活邮件
            Transport.send(message);
        } catch (MessagingException mex) {
          
   
            mex.printStackTrace();
        }
    }
}

Swagger测试

邮件就收到了 ===>

经验分享 程序员 微信小程序 职场和发展