zj
2025-11-05 9c6296d07a668b15b9794709172e28ce3d9603e2
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.nq.utils.email;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
 
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
 
/**
 * @program: dabaogp
 * @description:
 * @create: 2025-09-16 13:44
 **/
public class GmailSender {
 
    private static final String API_URL = "https://www.aoksend.com/index/api/send_email";
 
//    public static void main(String[] args) {
//        aokSend("9e0y5ycs8@tempmail.cn","verification code","33456");
//    }
    public static void aokSend(String to, String subject, String body) {
        // 创建一个CloseableHttpClient实例
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建一个HttpPost实例
            HttpPost httpPost = new HttpPost(API_URL);
            // 创建一个参数列表
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("app_key", "d7b800835c593a4c393d85521b72d6f3"));
            params.add(new BasicNameValuePair("template_id", "E_133166271351"));
            params.add(new BasicNameValuePair("to", to));
            params.add(new BasicNameValuePair("reply_to", ""));
            params.add(new BasicNameValuePair("alias", "Stock"));
            params.add(new BasicNameValuePair("data", "{\"code\":"+body+"}"));
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8");
            httpPost.setEntity(formEntity);
            // 发送请求并获取响应
            HttpResponse response = httpClient.execute(httpPost);
            // 读取响应内容
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
                System.out.println("Response: " + responseBody);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static void sendEmailAli(String to, String subject, String body) {
        // 初始化邮件发送器
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
 
        // 设置邮箱服务器配置
        mailSender.setHost("smtp.mxhichina.com");
        mailSender.setPort(465);
        mailSender.setUsername("ipcptvzpd@candyyun.com");
        mailSender.setPassword("hdj$KDL#994");
        mailSender.setProtocol("smtp");
        mailSender.setDefaultEncoding("UTF-8");
 
        // 设置JavaMail属性
        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable", "true");
        // 移除重复的socketFactory配置,避免冲突
 
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("ipcptvzpd@candyyun.com");
            message.setTo(to);
            message.setSubject(subject);
            message.setText(body);
 
            mailSender.send(message);
            System.out.println("验证码邮件发送成功!");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("邮件发送失败", e);
        }
    }
//    public static void sendEmailAli(String to, String subject, String body) {
//        // 初始化邮件发送器
//        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
//
//        // 设置邮箱服务器配置
//        mailSender.setHost("smtp.mxhichina.com"); // 阿里企业邮箱SMTP服务器
//        mailSender.setPort(465); // SSL端口
//        mailSender.setUsername("ipcptvzpd@candyyun.com"); // 发件邮箱
//        mailSender.setPassword("g83r022ll6"); // 邮箱密码或授权码
//        mailSender.setProtocol("smtp");
//        mailSender.setDefaultEncoding("UTF-8");
//
//        // 设置JavaMail属性
//        Properties props = mailSender.getJavaMailProperties();
//        props.put("mail.smtp.auth", "true");
//        props.put("mail.smtp.ssl.enable", "true");
//        props.put("mail.smtp.socketFactory.port", "465");
//        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//        props.put("mail.smtp.socketFactory.fallback", "false");
//
//        try {
//            SimpleMailMessage message = new SimpleMailMessage();
//            message.setFrom("ipcptvzpd@candyyun.com"); // 发件人
//            message.setTo(to);
//            message.setSubject(subject);
//            message.setText(body);
//
//            mailSender.send(message);
//            System.out.println("验证码邮件发送成功!");
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException("邮件发送失败", e);
//        }
//    }
 
 
 
    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);
    }
}