java给邮箱发送验证码

邮箱发送信息需要用到邮箱授权码 

获取qq邮箱授权码方式:

邮箱——>设置——>账户——>往下拖到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务——>获取授权码

java给邮箱发送验证码_第1张图片

java给邮箱发送验证码_第2张图片  

引入依赖

        
            org.apache.commons
            commons-email
            1.5
        

工具类:(测试过两种方式都可以,随意哪种)

package com.kgc.ymw.util;

import java.security.GeneralSecurityException;
import java.util.Properties;
import java.util.Random;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.util.MailSSLSocketFactory;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.junit.jupiter.api.Test;

/**
 * @author: BruceYoung
 * @date: 2023/5/11
 */
@SuppressWarnings({"all"})
public class EmailTest {
    private static String yzm;

    @Test
    public void send1() {
        String email = "[email protected]";//接收人邮箱
        //HtmlEmail方式
        sendEmail(email);
    }

    @Test
    public void send2() {
//        yzm = random1();
        try {
            //javax.mail方式(发送方的邮箱,qq邮箱中申请的16位授权码,接收人邮箱,邮件标题,邮件内容)
            sendMail("[email protected]", "授权码", "[email protected]", "名称", "

邀请您注册验证码:" + yzm + "

"); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (GeneralSecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 方式1:发送QQ邮件 */ public static String sendEmail(String email) { HtmlEmail send = new HtmlEmail();//创建一个HtmlEmail实例对象 // 获取随机验证码 yzm = random1(); String resultCode = yzm; try { send.setHostName("smtp.qq.com"); send.setAuthentication("[email protected]", "邮箱授权码"); //第一个参数是发送者的QQEamil邮箱 第二个参数是刚刚获取的授权码 send.setFrom("[email protected]", "名称");//发送人的邮箱为自己的,用户名可以随便填 记得是自己的邮箱不是qq // send.setSmtpPort(465); //端口号 可以不开 send.setSSLOnConnect(true); //开启SSL加密 send.setCharset("utf-8"); send.addTo(email); //设置收件人 email为你要发送给谁的邮箱账户 send.setSubject("标题"); //邮箱标题 send.setMsg("您的验证码为: " + resultCode + " ,五分钟后失效"); //Eamil发送的内容 send.send(); //发送 } catch (EmailException e) { e.printStackTrace(); } return yzm; } /** * 方式2:发送QQ邮件 * * @param sender 发送方的邮箱 * @param auth qq邮箱中申请的16位授权码 * @param to 接收人邮箱 * @param title 邮件标题 * @param content 邮件内容 */ public static String sendMail(String sender, String auth, String to, String title, String content) throws MessagingException, GeneralSecurityException, javax.mail.MessagingException { yzm = random1(); //创建一个配置文件并保存 Properties properties = new Properties(); properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.setProperty("mail.smtp.socketFactory.fallback", "false"); properties.setProperty("mail.smtp.port", "465"); properties.setProperty("mail.smtp.socketFactory.port", "465"); properties.setProperty("mail.host", "smtp.qq.com"); properties.setProperty("mail.transport.protocol", "smtp"); properties.setProperty("mail.smtp.auth", "true"); //QQ存在一个特性设置SSL加密 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.ssl.socketFactory", sf); //创建一个session对象 Session session = Session.getDefaultInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(sender, auth); } }); //开启debug模式 session.setDebug(true); //获取连接对象 Transport transport = session.getTransport(); //连接服务器 transport.connect("smtp.qq.com", sender, auth); //创建邮件对象 MimeMessage mimeMessage = new MimeMessage(session); //邮件发送人 mimeMessage.setFrom(new InternetAddress(sender)); //邮件接收人 mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); //邮件标题 mimeMessage.setSubject(title); //邮件内容 mimeMessage.setContent(content, "text/html;charset=UTF-8"); //发送邮件 transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); //关闭连接 transport.close(); return yzm; } //生成6位数 验证码 public static String random1() { String code = ""; Random rd = new Random(); for (int i = 0; i < 6; i++) { int r = rd.nextInt(10); //每次随机出一个数字(0-9) code = code + r; //把每次随机出的数字拼在一起 } System.out.println(code); return code; } }

前端页面参考:

//输入邮箱

    
        * 邮箱  
    
    
        
        

{{ email_error }}

//输入验证码 * 验证码  

如果要实现验证码在几分钟内失效的功能,则需要在后端把验证码存入redis中并设置一个有效期,取验证码就从redis里面取,点击提交时如果从redis中取得验证码为null则表示验证码失效。

你可能感兴趣的:(Java,VUE,java)