1
zj
2025-09-16 bb6fe349b56f454f9ec6f01c32f652ecd416f151
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.nq.utils.email;
 
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.SecureRandom;
import java.util.Properties;
 
/**
 * @program: dabaogp
 * @description:
 * @create: 2025-09-16 13:44
 **/
public class GmailSender {
 
    public static void sendEmail(String to, String subject, String body) {
        // 发件人Gmail地址
        String from = "coinzne.com@gmail.com";
        // 使用应用专用密码,而不是常规的Gmail密码
        String password = "pqupwyxoqedhxlfq";
 
        // 设置邮件服务器属性
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true"); // 启用TLS加密
 
        // 获取Session对象
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password);
            }
        });
 
        try {
            // 创建MimeMessage对象
            Message message = new MimeMessage(session);
 
            // 设置发件人
            message.setFrom(new InternetAddress(from));
 
            // 设置收件人
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
 
            // 设置邮件主题
            message.setSubject(subject);
 
            // 设置邮件正文
            message.setText(body);
 
            // 发送邮件
            Transport.send(message);
 
            System.out.println("邮件发送成功!");
 
        } catch (MessagingException e) {
            throw new RuntimeException("邮件发送失败: " + e.getMessage(), e);
        }
    }
 
    /**
     * 生成6位随机数字验证码(使用安全随机数生成器)
     * @return 6位数字字符串
     */
    public static String generateSecureSixDigitCode() {
        SecureRandom random = new SecureRandom();
        int code = random.nextInt(900000) + 100000;
        return String.valueOf(code);
    }
}