用QQ邮箱发送邮件验证码java

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

配置邮箱 

 

实现代码 

package com.example.demo.service;
import org.springframework.stereotype.Service;
 
import java.util.Properties;
import java.util.Random;
import javax.mail.*;
import javax.mail.internet.*;
 
@Service
public class QQEmailSender {
 
    public  void qq(String qq) {
        // 发件人邮箱地址和授权码
        String from ="your_email@qq.com";
        String password = "your_authorization_code";
        // 收件人电子邮箱
        String to = qq;
 
        // 指定发送邮件的主机为 smtp.qq.com
        String host = "smtp.qq.com";  //QQ 邮件服务器
 
        // 获取系统属性
        Properties properties = System.getProperties();
 
        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);
 
        properties.put("mail.smtp.auth", "true");
 
        // 获取默认的 Session 对象。
        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties,new Authenticator(){
            public PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(from, password); //发件人邮件用户名、授权码
            }
        });
 
        // 设置debug模式便于调试:
        session.setDebug(true);
 
        try{
            // 创建默认的 MimeMessage 对象。
            MimeMessage message = new MimeMessage(session);
 
            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));
 
            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));
 
            // Set Subject: 头字段
            message.setSubject("大可","UTF-8");
 
            // 发送 HTML 消息, 可以插入html标签
 
            String generatedCode = random1();   // 假设后台生成的验证码
            String emailBody = vericodeHtml.replace(":data=\"123456\"", ":data=\"" + generatedCode + "\"").replace("1EM456", generatedCode); //将发送页面的验证码改为后台生成的验证码
 
            message.setText(emailBody, "UTF-8", "html");
 
            // 发送消息
            Transport.send(message);
            System.out.println("Sent message successfully....");
        }catch (MessagingException mex) {
            mex.printStackTrace();
        }
 
    }
    //生成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;
    }
 
    public static String vericodeHtml = "<!DOCTYPE html>\n" +
            "<html lang=\"en\">\n" +
            "<head>\n" +
            "    <meta charset=\"UTF-8\">\n" +
            "    <title>邮箱验证码</title>\n" +
            "    <style>\n" +
            "\n" +
            "        .main {\n" +
            "            margin: 10px auto;\n" +
            "            width: 520px;\n" +
            "\n" +
            "            border-top: 4px solid #9373EE;\n" +
            "            padding: 24px 24px 40px;\n" +
            "            border-radius:0 0 8px 8px;\n" +
            "            box-shadow: 0px 0px 1px;\n" +
            "        }\n" +
            "\n" +
            "        .title {\n" +
            "            margin: 80px auto 32px;\n" +
            "            font-size: 32px;\n" +
            "            font-weight: 600;\n" +
            "            line-height: 45px;\n" +
            "            letter-spacing: 0px;\n" +
            "\n" +
            "        }\n" +
            "\n" +
            "        .note {\n" +
            "            margin: 0 auto;\n" +
            "            font-size: 18px;\n" +
            "            line-height: 1.4;\n" +
            "            left: 0px;\n" +
            "            top: 77px;\n" +
            "            font-weight: 400;\n" +
            "        }\n" +
            "\n" +
            "        .code {\n" +
            "            padding: 16px;\n" +
            "            text-align: center;\n" +
            "            background: rgba(147, 115, 238, 0.04);\n" +
            "            border-radius: 4px;\n" +
            "            font-weight: 600;\n" +
            "            font-size: 24px;\n" +
            "            line-height: 140%;\n" +
            "            color: #9373EE;\n" +
            "            margin: 24px 0;\n" +
            "            letter-spacing: 1px;\n" +
            "        }\n" +
            "\n" +
            "        .claim ul {\n" +
            "            margin-top: 34px;\n" +
            "            margin-bottom: 40px;\n" +
            "            font-size: 13px;\n" +
            "            line-height: 1.6;\n" +
            "            color: #5c5c5c;\n" +
            "            padding: 25px 0;\n" +
            "\n" +
            "        }\n" +
            "\n" +
            "        .claim ul li {\n" +
            "            color: rgba(24, 24, 25, 0.42);\n" +
            "            line-height: 30px;\n" +
            "        }\n" +
            "\n" +
            "        .footer {\n" +
            "            font-size: 13px;\n" +
            "            line-height: 1.6;\n" +
            "            color: #5c5c5c;\n" +
            "            padding: 25px 0\n" +
            "        }\n" +
            "        .title,.note,.claim,.footer {\n" +
            "            text-align: center;\n" +
            "        }\n" +
            "    </style>\n" +
            "</head>\n" +
            "<body>\n" +
            "<div class=\"main\">\n" +
            "    <div class=\"title\">邮箱账号验证码</div>\n" +
            "    <div class=\"note\">你正在进行邮箱验证操作,验证码为:</div>\n" +
            "    <div class=\"code\" :data=\"123456\">1EM456</div>\n" +
            "\n" +
            "    <div class=\"claim\">\n" +
            "        <ul style=\"list-style: none;\">\n" +
            "            <li style=\"list-style: none;\">此验证码 15 分钟内有效</li>\n" +
            "            <li style=\"list-style: none;\">如非本人操作</li>\n" +
            "            <li style=\"list-style: none;\">转给他人将导致账号被盗和个人信息泄漏,谨防诈骗</li>\n" +
            "        </ul>\n" +
            "    </div>\n" +
            "\n" +
            "    <div class=\"footer\">\n" +
            "        <a href=\"\" target=\"_blank\" style=\"color: #9373EE; text-decoration: none;\"></a>\n" +
            "    </div>\n" +
            "</div>\n" +
            "</body>\n" +
            "</html>";
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先需要引入JavaMail API和Java Activation Framework (JAF)的包。 以下是一个简单的Java代码示例,用于从QQ邮箱发送验证码邮件: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { final String username = "your_qq_email@qq.com"; final String password = "your_email_password"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.qq.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("your_qq_email@qq.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient_email@example.com")); message.setSubject("Verification Code"); int code = 123456; // replace with actual code message.setText("Your verification code is: " + code); Transport.send(message); System.out.println("Code sent successfully!"); } catch (MessagingException e) { throw new RuntimeException(e); } } } ``` 在上面的代码中: - 将`your_qq_email@qq.com`替换为您的QQ邮箱地址。 - 将`your_email_password`替换为您的QQ邮箱密码。 - 将`recipient_email@example.com`替换为收件人的电子邮件地址。 - 将`123456`替换为实际的验证码。 运行代码后,您应该能够在收件人的邮箱中收到一封包含验证码的电子邮件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Y2000104

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值